Global Methods in C# 3.0

time to read 2 min | 273 words

As you probably know, you can't define a stand alone method in C#, It must always reside within a class. C# 3.0 adds extention methods to the lagnague, which allows you to add methods to existing classes.

We can take advantage of that to implement a "global" method, like this:

public static class GlobalMethods
{
   public static bool IsNull(this object ignored, object obj)
   {
       return obj != null;
   }
}

Now I can use this like this:

if (IsNull(foo)) 

This is not really useful for this case, but it is a very big step for fluent interfaces. At the moment having to have a class name prefix is somewhat of an issue with fluent interfaces. Which sometimes doesn't make sense in the sentence structure we are trying to create.