Monday, July 21, 2008

Extension Methods - Cool

Since the first release of the .NET Framework (2002), Microsoft as significantly improved the quality and features of the framework. Also, the compilers and the Visual Studio have evolved in a fantastic way.

One of the many features introduced in VS2008 (with the new compilers) was the Extension Methods. What are they? A cute little thing! Ok, technically speaking, I prefer to quote Scott Gu:

“Extension methods allow developers to add new methods to the public contract of an existing CLR type, without having to sub-class it or recompile the original type. Extension Methods help blend the flexibility of “duck typing” support popular within dynamic languages today with the performance and compile-time validation of strongly-typed languages. Extension Methods enable a variety of useful scenarios, and help make possible the really powerful LINQ query framework that is being introduced with .NET as part of the “Orcas” release.”

But what’s happening behind the scene? Is this complicated? Well, lets start by looking at the extension methods with one example from Scott Gu. Have you ever wanted to check if a string is in some way valid? For example, have you ever wanted to check if a string is a valid e-mail address or “postal code”? Before extension methods, you’d probably write something like this:

(click to enlarge)

As you can see, there’s a static class with static methods to validate the string, which is passed as a parameter. But we’re trying to check if a string is in some way valid, so, shouldn’t we have this capability on the string type? Well, this is Extension Methods. Here’s the version using extension methods:

(click to enlarge)

Cool! You actually call a method from the string type! The “this” keyword tells the compiler that the extension method is to be applied to string type. However, what’s happening behind the scenes? Well, if we compile both the examples we will notice that it’s the C# compiler that’s “doing the trick”. Here’s the IL for the first example:

(click to enlarge)

And here’s the IL for the second example using extension methods:

(click to enlarge)

As you may have noticed, the compiler actually transformed the extension method in a normal method call, generating a class. From the CLR point of view, everything is still the same (there’s one new flag).


This is “the big picture”. However, the extension methods can be used with every base class or interface. Basically, it can be applied to any type. And this is fantastic. The built-in LINQ Extension methods use this feature. Once you get used to some of these “syntactical sugar” improvements, you’ll get addicted.

There are so much new cool features with the new compilers that come with VS2008 that the best is to take a look at Scott Gus’ blog. Despite the fact that he normally doesn’t look at the generated IL, he discusses important issues and his speech is very easy to understand. It’s a fantastic source of information.