Extension method

From Wikipedia, the free encyclopedia

Extension method is the name for a programming language feature of C# 3.0 and VB.NET 9.0.

Contents

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. Heretofore, 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.

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

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[] reversedChars = new char[input.Length];
        for (int i = input.Length - 1, j = 0; i >= 0; --i, j++)
        {
           reversedChars[j] = input[i ];
        }
        return new String(reversedChars);
     }
  }

Advanced Search
Included Web Search Engines


Safe Search

close

Top Matching Results

Occasionally Search.com will highlight specialized results that are based on the context of your query. Examples of specialized results include specific links to news, images, or video.

Top Matching Results may highlight information from other Search.com pages, content from the CNET Network of sites, or third party content. The listings are based purely on relevance. Search.com does not receive payment for listings in this section but our partners that provide this data may get paid for listing these products.

Sponsored Links

This section contains paid listings which have been purchased by companies that want to have their sites appear for specific search terms and related content. These listings are administered, sorted and maintained by a third party and are not endorsed by Search.com.

Search Results

Search.com sends your search query to several search engines at one time and integrates the results into one list which has been sorted by relevance using Search.com's proprietary algorithm. You can customize the list of search engines included in your metasearch from the preferences.

The search engines that are used in your metasearch may allow companies to pay to have their Web sites included within the results. To view the Paid Inclusion policy for a specific search engine, please visit their Web site. Search.com does not accept payment or share revenue with any search engine partner for listings in this section.