Secrets of Extension Methods: Part I

Most of us are familiar with C# new feature Extension methods. But, in this article I am going to discuss detailed implementation and restrictions on Extension Methods. I designed this application in Visual Studio 2008 using Console Applications.

Introduction:

Extension methods allows us to define new methods on existing types, which can be either .NET type or user defined class. We can define new methods without extending or deriving existing type. Internally, extension methods are treated as class or static methods. But, it will be called on instance of a class.

Use of Extension Methods in .NET Framework:


Extension methods are used heavily in most of the new .NET Classes [BCL]. LINQ uses this feature in its implementation. In coming articles, we will see how LINQ implements this feature.

Steps to create a sample Extension Method:

  1. Create a new Console Application and name it as ExtensionMethods. Add a class to it and name as Extensions.cs
     
  2. Make class as a public. Most of the cases, public visibility is better.
     
  3. Create a new method and name it as InitCap. This method takes a string as a input and makes first letter as Uppercase and returns complete string back.
     
  4. Make visibility as public and static in nature for the method.
     
  5. For Extension Methods, first parameter will be always the instance calling that method. So, we no need to pass it explicitly. It will be preceded by keyword this. The type of first parameter specifies on what kind of instances, this method is available.

    So, first parameter implies following things: 
    1. It should be preceded by keyword this.
       
    2. Its type specifies on what kind of objects, this method is available.
       
    3. No need to pass this parameter, when calling the extension method explicitly.
  6. Write this lines in InitCap method:

value = value[0].ToString().ToUpper()+value.Substring(1,value.Length-1);
return value;

This will make first letter capitalized and returns it back.

Finally, this class looks like this:



From this definition, we can say this method returns string and can be called on string objects [based on first parameter type].

Next go to program class and include the namespace in which the method is defined.
In this sample, no need to include the namespace. Since, both the client [program] code and extension methods are defined in same namespace.

So, create a string object and read the contents from console and call this extension method InitCap on that string object. Later, display the contents.

Finally, the program class will be like this:



Here, we called InitCap on the instance of string. So, it's easy to understand and improve readability of the code by using Extension Methods. We are calling InitCap without any parameters, since first parameter will be the object [input]

Even though, we called this method as an instance method. Internally, it will treat as a static method.

Restrictions:

If there is an already existing method with same signature as extension method in a type. Than, extension method will be ignored and never called.

First, it looks for inbuilt method. If it won't find a match, it will look for extension method. If it is not there, it will throw an error.

Internal Implementation:


If we disassemble this code, we can see the call to extension method will be like this:

Call string ExtensionMethods.Extensions::InitCap(string)

This will call InitCap method present in Extensions Class.

Summary:

An extension method allows us to add new method on any type without redefining or deriving it. Create a static class and static methods with first parameter preceded by keyword this. Include the namespace in the client code and call it.

I am attaching the code for reference. I hope this will be useful for all.

Part 2

Up Next
    Ebook Download
    View all
    Learn
    View all