Pular para o conteúdo

Conheça Walt Disney World

Extension method

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 Oxygene with 2.0. 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. For client code written in C# and Visual Basic, there is no apparent difference between calling an extension method and the methods that are actually defined in a type.[1]

Contents

The problem

In programming, situations arise where it is necessary to add functionality to an existing class—for instance by adding a new method. Normally the programmer would modify the existing class's source code, but this forces the programmer to recompile all binaries with these new changes and requires that the programmer be able to modify the class, which is not always possible, for example when using classes from a third-party assembly. This is typically worked around in one of three ways, all of which are somewhat limited and unintuitive:

  1. Inherit the class and then implement the functionality in an instance method in the derived class.
  2. Implement the functionality in a static method added to a helper class.
  3. Use aggregation instead of inheritance.

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 forbid 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();

Current Visual Basic.Net solutions

In most ways the VB.net solution is similar to the C# solution above. However VB.net has a unique advantage in that it allows members to be passed in to the extension by reference (C# only allows by value). Allowing for the following;

Dim x as string = "some string value"
x.Reverse()

Because VB allows the source object to be passed in by reference it's possible to make changes to the source object directly, without need to create another variable. It's also more intuitive as it works in a consistent fashion to existing methods of classes.

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 Utility
{
  public static string Reverse(this string input)
  {
    char[] chars = input.ToCharArray();
    Array.Reverse(chars);
    return new String(chars);
  }
}

In the definition, the modifier 'this' before the first argument specifies that it's an extension method (in this case to the type 'string'). In a call, the first argument is not 'passed in' because it's already known as the 'calling' object (the object before the dot).

The major difference between calling extension methods and calling static helper methods is that static methods are called in prefix notation, whereas extension methods are called in infix notation. The latter 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)

Naming Conflicts in Extension methods and Instance methods

In C# 3.0, both an instance method and an extension method with the same signature can exist for a class. In such a scenario, the instance method is preferred over the extension method. Neither the compiler nor the Microsoft Visual Studio IDE warns about the naming conflict. Consider this C# class, where the GetAlphabet() method is invoked on an instance of this class:

class AlphabetMaker 
{
    public void GetAlphabet()       
    {                               //When this method is implemented,
        Console.WriteLine("abc");   //it will shadow the implementation
    }                               //in the ExtensionMethods class.
}
 
static class ExtensionMethods
{
    public static void GetAlphabet(this AlphabetMaker am)   
    {                               //This will only be called                       
        Console.WriteLine("ABC");   //if there is no instance
    }                               //method with the same signature.   
}

Result of invoking GetAlphabet() on an instance of AlphabetMaker if only the extension method exists:

ABC

Result if both the instance method and the extension method exist:

abc

See also

References

External links

Personal tools
  • Log in / create account
Namespaces

Variants
Actions
Navigation
Toolbox
Print/export