IsNull Extension Method
I'm strongly considering adopting the use of an IsNull extension method in my .NET 3.5 coding projects. A quick search to see what others have to say about this revealed a new web site dedicated to extension methods, which includes this IsNull method ready to go:
public static bool IsNull(this object source)
{
return source == null;
}
The String class supports the IsNullOrEmpty() method now, but you have to pass it your instance yourself. This is another good candidate for Extension Method treatment. Brad Wilson posted about this earlier this year, and received some good comments, none of which seem to indicate that these are evil or icky.
From the ExtensionMethod.Net site, here is an example of the String check that I use so frequently:
public static bool IsNotNullOrEmpty(this string input) {
return !String.IsNullOrEmpty(input);
}
There are actually quite a few cool ones for strings. I think I like having Format as an extension as well:
using System;
using System.Linq
public static class StringExtensions
{
public static string Format(this string format, object arg, params object[] additionalArgs)
{
if (additionalArgs == null || additionalArgs.Length == 0)
{
return string.Format(format, arg);
}
else
{
return string.Format(format, new object[] { arg }.Concat(additionalArgs).ToArray());
}
}
}
Update: The Format extension method doesn't work as-is. I've renamed mine to Formatx(), which works.




Comments
Brendan Enrick said on 03 Jul 2008 at 1:00 PM
I had been planning on writing that for a while and had never gotten around to it. Crazy useful extension method.
That site looks awesome.
I expect I'll be using it often.
Mike said on 14 Jul 2008 at 12:02 PM
LOL - WTF is a "pubic" static method?
ssmith said on 14 Jul 2008 at 12:09 PM
@Mike - Hey, that's what was at the source site, what can I say... :) I thought I'd corrected that in my version but apparently I missed it.
Mike said on 14 Jul 2008 at 12:20 PM
No problem, just tickled me when I saw it.
Say - have you tried using that "Format" extension method? My compiler complains about it with the following message:
Member 'string.Format(string, object)' cannot be accessed with an instance reference; qualify it with a type name instead...
If I change the name of the extension method to anything else (i.e. 'Format2') it seems to work fine but sort of defeats the purpose.
Just wondering if I was doing something dumb.
ssmith said on 14 Jul 2008 at 12:36 PM
@Mike,
No, I ran into the same problem. Again, the code in the post reflects what I found on the source web site, not what actually ended up working for me. I went with "Formatx()" but Format2 would work just as well. Unfortunate, but it's still a handy extension to have.
Oskar Austegard said on 27 Jul 2008 at 8:52 PM
@Steve (and Mike): You may like the extension method I wrote the other day, which lets you use a lexical format string to inject an object, IDictionary or hashtable's property/key values into a string: mo.notono.us/.../c-stringinject-
Also, I was kinda surprised that an extension method would work for a null instance, but then I remembered that it really is just syntactic sugar converting back to a static in the compiler... Cool.