On Friday, 25 January 2013 at 15:13:21 UTC, Jacob Carlborg wrote:
The secret is to add "this" in front of the argument type you want to the method to extend:

namespace ExtensionMethods
{
    public static class MyExtensions
    {
        public static int WordCount(this String str)
        {
            return str.Split(new char[] { ' ', '.', '?' },
StringSplitOptions.RemoveEmptyEntries).Length;
        }
    }
}

using ExtensionMethods;

string s = "Hello Extension Methods";
int i = s.WordCount();

http://msdn.microsoft.com/en-us/library/vstudio/bb383977.aspx

Good. Main questions remain though: is property syntax allowed on extension methods? Is WordCount(s) call allowed?

Reply via email to