25 January 2014

extension methods in c#

Extension methods adds a method to class without modifying the class definition, so it's useful in adding method to a third party class within a namespace scope.

Uday is c# developer, he is using third party library in his project. A class in third party library, doesn't have a method, what Uday needs for his requirement. He cannot modify the third party library class to add the method what he needs. Using extension methods one can add method to class without modifying the class itself; Let's see how!

Go through the below tutorial topics to understand extension methods in c# in detail.

  1. Code sample in C# without using extension methods
  2. C# code example with extension methods
  3. How extension method works?
  4. extension methods in LINQ

Code sample in C# without using extension methods

// Class defined in Contracts.dll assembly.
// Class defined in Contracts.dll assembly.
public class Customer
{
  public string FirstName {get;set;}
  public string LastName {get;set;}
  public DateTime DOB {get;set;}

public string GetDisplayInfo()
  {
   return this.FirstName+ " "+this.LastName+ 
                   "born on" + this.DOB.ToString();
  }
}

The above Customer class has GetDisplayInfo method, but developer using the class also needs a method just to display customer full name. Such method doesn't exist on the Customer class and cannot edit the Customer class itself to include the method required. The reasons could be:

  • Such class is a.NET library class.
  • The class is in some third party library.
  • The new method in need is very specific to a requirement and may not be useful to rest of the consumers of the Customer class.

Then developer will mostly relent to static method in a static helper class like below:

// Helper class defined in application assembly
public static class CustomerHelper
{
   public string GetFullName(Customer customer)
    {
     return customer.FirstName + customer.LastName;
    }
}

Usage of such static helper class would look like below:

Customer customer = new Customer();
string fullName = CustomerHelper.GetFullName(customer);

With static class helpers, we can get the required functionality. But we have to pass in the customer object to get the customer full name. Wouldn't it be nice? if we have a mechanism to get the full name by calling a method on the customer object itself so that all the calling code will become simple and also easy to read. But how can it be done? Answer is simple, use extension methods.

C# code example with extension methods

Let's rewrite the GetFullName method using extension methods in C#.

namespace CustomerUtility {
 public static class CustomerHelper  {
   public string GetFullName(this Customer customer) {
        return customer.FirstName + customer.LastName;
    }
  }
}
Here GetFullName is a extension method to Customer class and let's see understand, how extension method will be defined:
  1. Extension methods should always be created inside a static class. Here CustomerUtility is such static class.
  2. Extension method is a static method with special this parameter specifying the class to which we need the extension method. Here Customer is such class.
  3. Extension methods will be available only within the containing static class's namespace scope. Here CustomerUtility is such namespace. So to use any extension method you need to import respective namespace first.

Now, let's see how code will be simple and easy to read using extension method:

using CustomerUtility;
// Inside a method you need customer full name.
{
Customer customer = new Customer();
string fullName = customer.GetFullName();
}

Visual Studio showing the Extension methods

Visual Studio shows the extension methods with (extension) prefixed.

Extension methods display in Visual Studio IDE

How extension method works?

Extension method invocations gets bound to respective static methods during compilation. Open the assembly in ILDASM to view the IL(Intermediate Language); You will find that the IL code of the lines which invokes the extension method is same as that of direct static method invocation.

Recently I have posted another article explaining a practical example of Extension methods.

No comments:

Post a Comment