Extension method

This is an old revision of this page, as edited by Soumyasch (talk | contribs) at 05:07, 3 January 2008 (Extension methods: fix lang name). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

An extension method is a new language feature of C# starting with the 3.0 specification, as well as Visual Basic.NET starting with 9.0 and Chrome with 2.0.

The problem

Normally, in a situation where it is necessary to add more functionality to a class - for instance a new method - it would simply be a matter of modifying the class' source code and recompiling the binaries with these new changes to make this effect. However, in some cases, especially when it comes to classes that are built-in into the .NET-framework or reside in third-party assemblies, the programmer does not have access to the source code and is thus unable to change the behavior of the class directly. Hence, the programmer has been left with two other more limited and less intuitive (respectively) options:

  1. The first option is to inherit the class and then add the functionality.
  2. The second option is to make a new, separate class and add a static method that takes an object of the class type and returns a new object with the modification of choice.

Current C# solutions

The first option is in principle easier, but it is unfortunately limited by the fact that many classes restrict inheritance of certain members or forbids it completely. This includes sealed class and the different primitive data types in C# such as int, float and string. The second option, on the other hand, does not share these restrictions, but it may be less intuitive as it requires a reference to a separate class instead of using the methods of the class in question directly.


As an example, consider a need of extending the string class with a new reverse method whose return value is a string with the characters in reversed order. Because the string class is a sealed type, the method would typically be added to a new utility class in a manner similar to the following:

string x = "some string value";
string y = Utility.Reverse(x);

This may, however, become increasingly difficult to navigate as the library of utility methods and classes increases, particularly for newcomers. The ___location is also less intuitive because, unlike most string methods, it would not be a member of the string class, but in a completely different class altogether. A better syntax would therefore be the following:

string x = "some string value";
string y = x.Reverse();

Extension methods

The new language feature of extension methods in C# 3.0, however, makes the latter code possible. This approach requires a static class and a static method, as follows:

public static class Util
{
  public static string Reverse(this string input)
  {
    char[] chars = input.ToCharArray();
    Array.Reverse(chars);
    return new String(chars);
  }
}

The major difference between extension methods and normal, static helper methods is that static methods calls are prefix notation, whereas extension methods calls are in infix notation. This leads to more readable code when the result of one operation is used for another operation.

With static methods
HelperClass.Operation2(HelperClass.Operation1(x, arg1), arg2)
With extension methods
x.Operation1(arg1).Operation2(arg2)

In the current implementation, a member method with the same name as an extension method shadows the latter and makes it inaccessible. The IDE (Visual Studio) also does not warn about the naming conflict.

See also

References