I've had a chance over the last few days to play some more with the March CTP of Visual Studio Codename "Orcas" and with the features being added as a part of C# 3.0. Even while feeling utterly handicap in Visual Studio without CodeRush and Refactor! Pro, I've managed turn out an interesting little assembly that I intend to grow as time goes with the namespace TimRayburn.Extensions. As you might guess this assembly contains Extension methods to classes within the Framework.
In case you don't know what extension methods are, they are static methods in static classes which use a special syntax to indicate that they extend an existing class or interface. For instance you might write some code like:
1: public static string ToStringNullSafe(this object inStr)
3: if (inStr == null) return string.Empty;
6: string lStr = inStr.ToString();
7: if (lStr == null) return string.Empty;
8: else return lStr;
Which extends the class System.Object to contain a new method called ToStringNullSafe which, as you can see, returns String.Empty instead of throwing Exceptions when a the object being called is null or it's ToString value is null. The intellisense for this looks like the image to the right.
I intend to use TimRayburn.Extensions to contain extension methods which I've written and find useful. Version 0.1 addresses very simple problems, but over time I'm sure more complex functions will be added. For now there are two classes in the project FrameworkExtensions and EnsureExtensions.
Framework Extensions contains three methods today. ToStringNullSafe (as seen above) extends System.Object to provide a ToString method which will not throw exceptions on null. ToInt32 extends System.String to provide the ability to convert to a System.Int32 inline. Likewise ToDouble extends System.String for conversion to System.Double. These extensions are simple and without a unifying purpose other than that I thought they would come in handy. Nothing being done here, or in EnsureExtensions, that can't be done without extension methods, just easier and more reader friendly code.
EnsureExtensions contains many methods which extend either System.Object, System.String or objects which implement IComparable<T>. Every method in this class follows the naming convention EnsureX where X is something you would want to confirm about the datatype before using it in code. The first of these is EnsureNotNull, an extension to System.Object, which checks if the object being used is null, and if it is throws an ArgumentException but if not it simply returns the object. This allows for interesting pieces of code like:
1: public void MyExampleMethod(string inputData)
5: // Real code here now that arguments have been checked.
The observant among you might notice that this sort of methodology could also be extended to handle NUnit Assert's, and I assure you that has not gone unnoticed by me. Extension methods may very well provide some interesting Syntactic Glue for many things in the future. Not rocket science, but very cool, and in my opinion very readable.
You can download the code for this below, though you will need the CTP of Orcas to compile this code.