.NET 3.5: Extension Methods

This is the first post in a series of xx that will be about the preparation of a presentation for my HintTech colleagues, ‘LINQ and the related new features of .NET 3.5’. Today I start with the feature Extension Methods.

In the C# 3.0 Specification Extension Methods is defined as ‘static methods that can be invoked using instance method syntax, which make it possible to extend existing types and constructed types with additional methods’. In a simple example of a Person class that has a Name property, you might define an Extension Method using the following syntax:

namespace ExtensionMethodDemo.Domain.Extensions
{
  public static class PersonExtensions
  {
    public static void SayGoodbye(this Person person)
    {
      Console.WriteLine(person.Name + " : Goodbye");
    }
  }
}

Note that both the class and the method are declared as static and that the (first) parameter is defined as ‘this Person person’, which defines the type to which the extension method applies. When you invoke this method ‘the difficult’ way:

ExtensionMethodDemo.Domain.Extensions.PersonExtensions.SayGoodbye(person);

there is basically nothing new to Extension Methods. Leave out the ‘this’ in the parameter and you can do the same in .NET 1.0, 1.1 and 2.0. As with normal static methods, Extension Methods also have the same limitations, ie. you only have access to the public’s of the Person class.

One of the powers of Extension Methods is that (after you import the namespace) the extended method appears as an instance method, so you can invoke it directly on the instance of a class:

person.SayGoodbye();

It even appears in the IntelliSense, with an indication that it is an extension to the original class. Taking into account that Extension Methods can also be applied on all .NET Framework classes (including object, string, etc.), you can imagine that some really powerful stuff is possible. For example you might have a look at the In() example of Scott Guthrie, which is an extension method to object.

Besides using Extension Methods on existing classes it is also possible to create shared interface implementations. Using this you can implement a general part of an interface using Extension Methods, which can avoid duplicate code in classes that implement the interface while clients have access to all the methods.

In the example below an interface is defined that is extended by a shared interface implementation. Doing this the Person class only has to implement SaySomething and clients have access to both methods SaySomething and SayHello through the interface IPerson:

public interface IPerson
{
  void SaySomething(string text);
}

namespace ExtensionMethodDemo.Domain.Extensions
{
  public static class PersonExtensions
  {
    public static void SayHello(this IPerson person)
    {
      person.SaySomething("Hello");
    }
  }
}

In relation to LINQ the feature of Extension Methods is used for the extensions on IEnumerable and IQueryable, for example the Where() method. The advantage of implementing this as Extension Methods is that you only have the methods  in scope when you use LINQ, not when you just need the interface.