In my opinion, one of the most marginalized features of C# 3.0 are extension methods. The idea behind them is to provide a way to extend a class with new methods by allowing static methods to be called using instance method syntax. Here's a simple example of how they can be used:
The above code declares a ToInt32() extension method for the System.String class. In the Main() method, ToInt32() is called using instance method syntax on a local string variable. As expected, this program prints 1001 to the console when run.
One interesting side effect of extension methods is that they can be called on a null reference without raising a NullReferenceException. Let's expand the sample code a bit to show what I mean:
The updated sample doesn't raise a NullReferenceException when text.ToInt32() is called—even though text is a null reference. Instead, the method returns 0 and 1 is printed to the console.
Obviously, this side effect can be a bit of a land mine and it is good practice to allow NullReferenceExceptions to be thrown from extension methods so that everything works as expected. But, there are a couple of cases where this side effect can be used to great effect.
Over at Phil Haack's blog, there is a bit of discussion about the String.IsNullOrEmpty static method that was added in .NET Framework 2.0. In the comments section, Jon Galloway wrote the following: "I like the static method, but I wish there were a member method (which just called the static) as well. I'm always thrown when myString.IsNullOrEmpty() isn't defined..." The next comment states that this doesn't make sense because you can't call an instance method on a null reference. However, you can call an extension method on a null reference. Here is how you could achieve a String.IsNullOrEmpty() instance method that really works:
Thanks to the null-reference side effect of extension methods, the above program writes this to the console:
In my opinion, taking advantage of the side effect in this case is OK because the name of the method is descriptive. In addition, this allows us to add another method to the .NET framework that is sorely missing: Array.IsNullOrEmpty():
For my own forays into C# 3.0, I've placed both of these extension methods inside the System namespace. That way, all I have to do is reference the assembly containing my extensions and they will always be available:
Page rendered at Tuesday, January 06, 2009 12:35:48 AM (Eastern Standard Time, UTC-05:00)