Thursday, February 14, 2008

Extension method


Extension methods enable you to "add" methods to existing types without creating
a new derived type, recompiling, or otherwise modifying the original type.
Extension methods are a special kind of static method, but they are called as if
they were instance methods on the extended type.


The most common extension methods are the LINQ standard query operators that add
query functionality to the existing System.Collections..::.IEnumerable
and System.Collections.Generic.IEnumerable(T)
types. To use the standard query operators, first bring them into scope with a
using System.Linq directive. Then any type that
implements IEnumerable(T) appears to have instance methods such as GroupBy,
OrderBy, Average, and so on.


You can see these additional methods in IntelliSense statement completion when
you type "dot" after an instance of an IEnumerable(T) type such as List(T) or Array.


The following example shows how to call the standard query operator OrderBy
method on an array of integers. The expression in parentheses is a lambda
expression. Many standard query operators take lambda expressions as parameters,
but this is not a requirement for extension methods.




class ExtensionMethods2
{

static void Main()
{
int[] ints = { 10, 45, 15, 39, 21, 26 };
var result = ints.OrderBy(g => g);
foreach (var i in result)
{
System.Console.Write(i + " ");
}
}
}
//Output: 10 15 21 26 39 45

Extension methods are defined as static methods but are called by using instance method syntax. Their first parameter specifies which type the method operates on, and the parameter is preceded by the modifier.
Extension methods are only in scope when you explicitly import the namespace
into your source code with a using directive.

The following example shows an extension method defined for the System.String class. Note that it is defined inside a non-nested, non-generic static class:



namespace ExtensionMethods
{

public static class MyExtensions

{

public static int WordCount(this String str)

{
return str.Split( char[] { ' ', '.', '?' }, StringSplitOptions.RemoveEmptyEntries).Length;

}

}

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

In code you invoke the extension method with instance method syntax. However, the intermediate language (IL) generated by the compiler translates your code into a call on the static method. Therefore, the principle of encapsulation is not really being violated. In fact, extension methods cannot access private variables in the type they are extending.

In general, you will probably be calling extension methods far more often than implementing your own. Because extension methods are called by using instance method syntax, no special knowledge is required to use them from client code. To enable extension methods for a particular type, just add a using directive for the namespace in which the methods are defined. For example, to use the standard query operators, add this using directive to your code:



Binding Extension Methods at Compile Time


You can use extension methods to extend a class or interface, but not to override them. An extension method with the same name and signature as an interface or class method will never be called. At compile time, extension methods always have lower priority than instance methods defined in the type itself. In other words, if a type has a method named P_Operation(int i), and you have an extension method with the same signature, the compiler will always bind to the instance method. When the compiler encounters a method invocation, it first looks for a match in the type's instance methods. If no match is found, it will search for any extension methods that are defined for the type, and bind to the first extension method that it finds



General Guidelines



In general, I recommend that you implement extension methods sparingly and only
when you have to. Whenever possible, client code that must extend an existing
type should do so by creating a new type derived from the existing type.


When using an extension method to extend a type whose source code you cannot change, you run the risk that a change in the implementation of the type will cause your extension method to break.


If you do implement extension methods for a given type, remember the following
two points:

  • An extension method will never be called if it has the same signature as a
    method defined in the type.
  • Extension methods are brought into scope at the namespace level. For example, if
    you have multiple static classes that contain extension methods in a single namespace named Extensions, they will all be brought into scope by the using Extensions; directive.

Implementers of class libraries should not use extension methods to avoid
creating new versions of assemblies. If you want to add significant new
functionality to a library, and you own the source code, you should follow the
standard .NET Framework guidelines for assembly versioning


No comments: