C# 3.0 Extension Methods

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 Method Chaining
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"));



Ambiguity and Resolution

Namespaces
An extension method cannot be accessed unless the namespace is in scope (typically imported with a using statement).

Extension methods versus instance methods
Any compatible instance method will always take precedence over an extension method. In the following example, Test's Foo method will always take precedence—even when called with an argument x of type int:

class Test
{
public void Foo (object x) { } // This method
} // always wins

static class Extensions
{
public static void Foo (this Test t, int x) { }
}

The only way to call the extension method in this case is via normal static syntax; in other words, Extensions.Foo(…).

Extension methods versus extension methods
If two extension methods have the same signature, the extension method must be called as an ordinary static method to disambiguate the method to call. If one extension method has more specific arguments, however, the more specific method takes precedence.
To illustrate, consider the following two classes:

static class StringHelper
{
public static bool IsCapitalized (this string s) {...}
}
static class ObjectHelper
{
public static bool IsCapitalized (this object s) {...}
}


The following code calls StringHelper's IsCapitalized method:

bool test1 = "Perth".IsCapitalized();


To call ObjectHelper's IsCapitalized method, we explicitly must specify it:

bool test2 = (ObjectHelper.IsCapitalized ("Perth"));

0 comments


Subscribe to Developer Techno ?
Enter your email address:

Delivered by FeedBurner