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(thisobject inStr)
2: {
3: if (inStr == null) returnstring.Empty;
4: else
5: {
6: string lStr = inStr.ToString();
7: if (lStr == null) returnstring.Empty;
8: else return lStr;
9: }
10: }
.csharpcode-wrapper, .csharpcode-wrapper pre {
background-color: #f4f4f4;
border: solid 1px gray;
cursor: text;
font-family: consolas, ‘Courier New’, courier, monospace;
font-size: 8pt;
line-height: 12pt;
margin: 20px 0px 10px 0px;
max-height: 200px;
overflow: auto;
padding: 4px 4px 4px 4px;
width: 97.5%;
}
.csharpcode-wrapper pre {
border-style: none;
margin: 0px 0px 0px 0px;
overflow: visible;
padding: 0px 0px 0px 0px;
}
.csharpcode, .csharpcode pre, .csharpcode .alt {
background-color: #f4f4f4;
border-style: none;
color: black;
font-family: consolas, ‘Courier New’, courier, monospace;
font-size: 8pt;
line-height: 12pt;
overflow: visible;
padding: 0px 0px 0px 0px;
width: 100%;
}
.csharpcode pre {
margin: 0em;
}
.csharpcode .alt {
background-color: white;
}
.csharpcode .asp {
background-color: #ffff00;
}
.csharpcode .attr {
color: #ff0000;
}
.csharpcode .html {
color: #800000;
}
.csharpcode .kwrd {
color: #0000ff;
}
.csharpcode .lnum {
color: #606060;
}
.csharpcode .op {
color: #0000c0;
}
.csharpcode .preproc {
color: #cc6633;
}
.csharpcode .rem {
color: #008000;
}
.csharpcode .str {
color: #006080;
}
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)
2: {
3: inputData.EnsureNotNull().EnsureGreaterThan("Bravo").EnsureLessThan("Charlie");
4:
5: // Real code here now that arguments have been checked.
6: }
.csharpcode-wrapper, .csharpcode-wrapper pre {
background-color: #f4f4f4;
border: solid 1px gray;
cursor: text;
font-family: consolas, ‘Courier New’, courier, monospace;
font-size: 8pt;
line-height: 12pt;
margin: 20px 0px 10px 0px;
max-height: 200px;
overflow: auto;
padding: 4px 4px 4px 4px;
width: 97.5%;
}
.csharpcode-wrapper pre {
border-style: none;
margin: 0px 0px 0px 0px;
overflow: visible;
padding: 0px 0px 0px 0px;
}
.csharpcode, .csharpcode pre, .csharpcode .alt {
background-color: #f4f4f4;
border-style: none;
color: black;
font-family: consolas, ‘Courier New’, courier, monospace;
font-size: 8pt;
line-height: 12pt;
overflow: visible;
padding: 0px 0px 0px 0px;
width: 100%;
}
.csharpcode pre {
margin: 0em;
}
.csharpcode .alt {
background-color: white;
}
.csharpcode .asp {
background-color: #ffff00;
}
.csharpcode .attr {
color: #ff0000;
}
.csharpcode .html {
color: #800000;
}
.csharpcode .kwrd {
color: #0000ff;
}
.csharpcode .lnum {
color: #606060;
}
.csharpcode .op {
color: #0000c0;
}
.csharpcode .preproc {
color: #cc6633;
}
.csharpcode .rem {
color: #008000;
}
.csharpcode .str {
color: #006080;
}
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.
Download
TimRayburn.Extensions