My Photo

Technical Architect with over 15 years experience in a wide range of technologies.

@TheCodeKing

rss feed

One of the most useful yet little-known features to come out of C# 2.0 | Friday, September 28, 2007

Introduction

I'm constantly surprised by the number of developers who aren't aware of this handy piece of syntax. It's my favorite thing to come out of C# 2.0 and no developer should be without it.

Like the conditional (?:) operator's big brother... introducing for your coding pleasure...

The Null Coalescing Operator (??)

The null-coalescing-operator is a brilliant new terse operator that provides syntax for beautifully concise if statements. Essentially it returns the left-hand-side of the ?? operator, unless null, in which case it executes and returns the right-hand-side of the operator. This may be a statement or a variable reference. Lets jump straight to some examples.
    // used inline outputs the value foo or if null returns Undefined
    Console.WriteLine("The value of foo is " 
                                 + (foo ?? "Undefined") + ".");
    
    Input:  foo = "24";
    Output: The value of foo is 24.
    
    Input:  foo = null;
    Output: The value of foo is Undefined.
The operator is right-associative meaning statements can be chained together; thus returning the first non-null instance.
    // assigns foo to the first non-null instance, 
    // else returns Undefined
    string foo = foo1 ?? foo2 ?? foo3 ?? foo4 ?? "Undefined";
    
    Console.WriteLine("The value of foo is " + foo + ".");

    Input:  foo1 = null;
            foo2 = null;
            foo3 = null;
            foo4 = null;
    Output: The value of foo is Undefined.
    
    Input:  foo1 = null;
            foo2 = "foo2";
            foo3 = null;
            foo4 = "foo4";
    Output: The value of foo is foo2.
Handling null ViewState references.
    // try to assign ViewState value as an int, 
    // else if null assign 123
    int foo = (int?)ViewState["foo"] ?? 123;
    
    Response.Write("The value of foo is " + foo + ".");
    
    Input:  ViewState["foo"]=1;
    Ouput:  The value of foo is 1.  
    
    Input:  ViewState["foo"]=null;
    Ouput:  The value of foo is 123.  
And my personal favorite, on demand field instantiation.
    private IList<string> foo;

    public IList<string> Foo
    {
        get
        {
            return foo ?? (foo = new List<string>());
        }
    }

The Rules

To use the null-coalescing-operator there are some compile-time ground rules.

  • The left-hand-side must evaluate to a reference or nullable type.
  • All evaluated statements must be of matching type, unless they can be implicitly converted.

Summary

As you can see from the examples above, this little gem is very powerful and the possibilities are endless. Of course the benefits are purely syntactical, but it helps keep the code clean and easier to follow. I hope you enjoy it as much as I do.


1 comment:

Verious said...

That is a great tip!

Post a Comment