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.