C# 3.0 has a feature that allows you to extend an existing class, even one you didn't write. The extensions allow you to add instance methods (sorry, static methods not supported) to any class.
What I wasn't sure was whether or not you could call these extension methods when you have a null instance of the object, since they're instance methods. The C++ guy in me said "sure, that should be legal", and the C# guy in me said "it's probably illegal, and that's too bad". Amazingly, the C++ guy in me won!
This code executes perfectly:
using System; public static class MyExtensions { public static bool IsNull(this object @object) { return @object == null; } } public class MainClass { public static void Main() { object obj1 = new object(); Console.WriteLine(obj1.IsNull()); object obj2 = null; Console.WriteLine(obj2.IsNull()); } }
When you run it, it prints out "False" and "True". Excellent!