Extension methods allow an existing type to be extended with new methods,
without altering the definition of the original type. An extension method
is a static method of a static class, where the this
modifier is applied to the first parameter.
The type of the first parameter will be the type that is extended. For
example:
public static class StringHelper { public static bool IsCapitalized (this string s) { if (string.IsNullOrEmpty (s)) return false; return char.IsUpper (s[0]); } }
The IsCapitalized
extension
method can be called as though it were an instance method on a string, as
follows:
Console.Write ("Perth".IsCapitalized());
An extension method call, when compiled, is translated back into an ordinary static method call:
Console.Write (StringHelper.IsCapitalized ("Perth"));
Interfaces can be extended, too:
public static T First<T> (this IEnumerable<T>
sequence)
{
foreach (T element in sequence)
return element;
throw new InvalidOperationException ("No elements!");
}
...
Console.WriteLine ("Seattle".First()); // S
Extension methods, like instance methods, provide a tidy way to chain functions. Consider the following two functions:
public static class StringHelper { public static string Pluralize (this string s) {...} public static string Capitalize (this string s) {...} }
x
and y
are equivalent and both evaluate to "Sausages"
, but x
uses extension methods, whereas y
uses static methods:
string x = "sausage".Pluralize().Capitalize(); string y = StringHelper.Capitalize (StringHelper.Pluralize ("sausage"));