Welcome to EMC Consulting Blogs Sign in | Join | Help

David Wynne's Blog

Handy Code: ThrowIfNull Extension Method

Checking whether an object is Null before using it and throwing a Null Reference Exception if it is indeed Null, is a pretty repetitive task.  Seeing code like this is not unusual:

private readonly List<string> people;
private readonly List<string> tasks;

public Without(List<string> people, List<string> tasks)
{
    // Null Checking
    if (people == null)
    {
        throw new NullReferenceException("people is Null");
    }

    if (tasks == null)
    {
        throw new NullReferenceException("tasks is Null");
    }

    // Field Initialisation
    this.people = people;
    this.tasks = tasks;

    // Actual code we care about here.
}

This kinda get’s my aesthetic coding back up, it’s repetitive, wordy and defocuses our code from the job of doing something useful.  Enter a generic extension method to take away the pain:

public static T ThrowIfNull<T>(this T value, string variableName) where T : class
{
    if (value == null)
    {
        throw new NullReferenceException(string.Format("Value is Null: {0}", variableName));
    }

    return value;
}

Now we can refactor our original code to the following:

private List<string> people;
private List<string> tasks;

public With(List<string> people, List<string> tasks)
{
    // Field Initialisation
    this.people = people.ThrowIfNull("people");
    this.tasks = tasks.ThrowIfNull("tasks");

    // Actual code we care about here.
}

What’s more, because our extension method ultimately returns the object upon which it’s being enacted, we can chain methods to make even more succinct code:

private void AlternativeUse(List<string> places)
{
    places.ThrowIfNull("places").Add("Home");
}

Happy coding.

Published 13 February 2009 19:19 by David.Wynne
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

Matthew Adams said:

Neat. I'd suggest you throw an ArgumentNullException (with the relevant argument passed as a parameter), rather than NullReferenceException. It kind of indicates that the caller has done something wrong, rather than something internal to the method just blowing up.

February 13, 2009 22:32
 

Christopher Steen said:

Link Listing - February 23, 2009

February 24, 2009 05:14
 

Christopher Steen said:

ASP.NET ASP.NET ListView: Displaying Hierarchical Data [Via: Adam Pooler ] ASP.NET Dynamic Data Resources...

February 24, 2009 05:15
 

James Curran said:

To expand on Matthew's comment, if you are going to throw NullReferenceException, then ThrowifNull becomes completely pointless.  You'd get the exact same response with just:

private void AlternativeUse(List<string> places)

{

   places.Add("Home");  

}

February 25, 2009 14:45
 

David.Wynne said:

@Matt - yup you're right!

@James - you're right too, in the case of AlternativeUse.  I think it's still a valid use in the constructor.

February 25, 2009 14:55

Leave a Comment

(required) 
(optional)
(required) 
Submit
Powered by Community Server (Personal Edition), by Telligent Systems