Are Extension Methods Useful for Normal Programming Tasks?

With the release of Visual Studio 2008, Microsoft also released C# 3.0.  The new language version includes some nice programmer productivity features like Anonymous Properties, Lamda Expressions, and Extension Methods.  I will be concentrating on Extension Methods for this post.

Extension methods allow a programmer to define a set of static methods in a static class that can be called on any object that matches the parameter type of the method.  Note that the methods must be in a static class or they may not be used as Extension methods.  For instance, I have defined the following static class named SampleExtension:

The only difference between ExtMethod1() and any other static method is the use of the keyword this.  The use of this string is basically saying that the Extension Method ExtMethod1() can operate on any object of type string and that the string itself should be passed to the method as a parameter. 

To use my new Extension Method, all I need to do is import the StringExtensions namespace and start calling the new method on string data types.

As you can see, Extension Methods make a class appear to have new methods that are not in the original class definition.  Visual Studio will even give you Intellisense for the Extension Methods in the namespaces you are referencing. 

Some of the dynamic languages, like Ruby for instance, have included this type of functionality for quite some time.  In fact, the Ruby on Rails framework makes extensive use of this type of functionality.  LINQ, new to .Net 3.5, utilizes Extension Methods to all developers to incorporate their collection types directly in the syntax of the queries.

Well printing to the console is nice, but not very useful in real life.  The method below demonstrates create an object instance based on string representation of the object type:

Below, I have added an example of calling this method.

The final aspect of Extension Method syntax I wanted to discuss is that, just like other static methods, Extension Methods may accept other parameters in addition to the object that is targeted for extension.  Below I have added a new overload to ExtMethod1() that accepts an additional string parameter:

Calling the method overload is just like calling any normal method:

Finally, developers might be tempted to put these extension methods operating on System.* types under the System namespace, thinking that it is more convenient for users of their methods.  I believe you should refrain from this and instead keep your Extension Methods in specialized namespaces.  This will keep users of your libraries from inadvertently using your methods when they do not intend to.  After all, every C# class imports the System namespace automatically.

In my opinion, Extension Methods are probably most useful for framework and reusable library development.  Now that Visual Studio 2008 is officially released, C# 3.0 will start gaining more usage, and we will see how people use this new capability.

I hope you find this brief introduction to Extension Methods useful.  You can find the sample code here.

Tags: , , ,


5 Responses to “Are Extension Methods Useful for Normal Programming Tasks?”

  1. twalker on December 11th, 2007 8:21 pm


    In the text above you stated that with the release of VS 2008 they also release c# 3.0 but I thought it was .net 3.5. Which one is correct? Nice blog site!


  2. Darren Stokes on December 12th, 2007 5:27 am

    Well, technically both are correct. Microsoft has decided to make the version numbering scheme for the entire development tool suite as complicated as possible.

    C# is versioned separately, according to the language standand. Since the C# language did not change for .Net 3.0, the version of the language was still 2.0. The language additions that are included in 2008 have revised the language version to 3.0.

    The .Net libary edition that is new with 2008 is .Net 3.5.

  3. twalker on December 12th, 2007 7:11 am

    Hey, thanks for the clarification!

  4. celpjefscycle on January 12th, 2008 1:16 am

    Thanks for information.
    many interesting things

  5. zak on October 7th, 2008 5:16 pm

    Nice site, and nice intro to extensions. I’ve also started liking them. You can work an extension object into your logging solution, which makes logging exceptions as easy as exceptionInstance.Log();. I’ve written about it here