Geeks With Blogs
John Conwell: aka Turbo Research in the visual exploration of data
I get a bit sick of checking for null on my IEnumerable objects before doing a foreach over them.  In my opinion I think the CLR should check if the list is null, and if it is just exit out of the foreach iteration as if there were no items in it.

Well, I was goofing around with Extension Methods a bit and figured out how to get this kind of functionality (sort of).

Now unfortunatly Extension Methods cant override an existing method on a type, so I cant just create a new GetEnumerator extension method (well, actually i can make one, but it wont get called).  But I can create a new method that returns IEnumerable, and just call the foreach on it.

So in order to do this, first add this class to your code

public static class MyExtnesionMethods
{
    public static IEnumerable<T> Enum<T>(this IEnumerable<T> input)
    {
        if (input != null)
        {
            foreach (var t in input)
            {
                yield return t;
            }
        }
        else
        {
            yield break;
        }
    }
}

Now, anything that inherits from IEnumerable<T> will have the Enum method.  Then all you have to do is call foreach on someClass.Enum(), even if someClass is null.  Below is an example of ho this works.

static void Main(string[] args)
{
    List<string> names = new List<string>()
        {"john", "kim", "jean", "brent"};

    //iterate names using stock enumerator
    foreach (string name in names)               
        Console.WriteLine(name);
   
    //iterate names using extension method
    foreach (string name in names.Enum())
        Console.WriteLine(name);

    names = null;

    //oh man!  I have to check for null...I hate that
    if (names != null)
        foreach (string name in names)
            Console.WriteLine(name);

    //Yea!  I dont have to check for null anymore!
    foreach (string name in names.Enum())
        Console.WriteLine(name);
}

The extension method uses the "yield return" and "yield break" iterator syntax to let the foreach either spin over the IEnumerable if its not null, or if it is null, "yield break" returns false from the IEnumerable.MoveNext which tells the foreach that there are no more items in the list so it should break out of the loop.

So, no more null checks!

<Update>
A reader commented that this could be optimized by using the static method Enumerable.Empty<T>.  This would save an object instance from being created by the yield return functionality.  The new and improved Extension Method is as follows:

public static IEnumerable<T> Enum<T>(this IEnumerable<T> input)
{
    return input ?? Enumerable.Empty<T>();
} Posted on Thursday, May 29, 2008 4:14 PM Ramblings | Back to top


Comments on this post: No more null checking on your IEnumerables before you iterate over them

# re: No more null checking on your IEnumerables before you iterate over them
Requesting Gravatar...
Your method could use an optimization to avoid creating new objects at runtime (assuming input != null is the general case):

public static IEnumerable<T> Enum(this IEnumerable<T> input) {
return input != null ? input : Enumerable.Empty<T>();
}
Left by Jay on May 29, 2008 11:07 PM

# re: No more null checking on your IEnumerables before you iterate over them
Requesting Gravatar...
nice...I hadnt seen Enumerable.Empty<T>. That works perfectly!
Left by john on May 30, 2008 8:51 AM

# re: No more null checking on your IEnumerables before you iterate over them
Requesting Gravatar...
Couldn't you make it even simpler as:

public static IEnumerable<T> Enum(this IEnumerable<T> input) {
return input ?? Enumerable.Empty<T>();
}

Good stuff John and Jay
Left by Richard Gardiner on May 30, 2008 9:13 AM

# re: No more null checking on your IEnumerables before you iterate over them
Requesting Gravatar...
Yup...thats what I ended up with. Thanks for pointing it out.
Left by john on May 30, 2008 9:37 AM

# re: No more null checking on your IEnumerables before you iterate over them
Requesting Gravatar...
Awesome extension method guys. Thx very much
Left by Umesh on Nov 22, 2009 1:57 PM

Your comment:
 (will show your gravatar)


Copyright © John Conwell | Powered by: GeeksWithBlogs.net