you're reading...
.NET 2.0

Null Coalescing operator

While reading my feeds today I came across a post by Scott Guthrie showing how to use the null coalescing operator with LINQ. What really caught my eye was a feature I had never heard of before; the null coalescing operator. A quick look at Scott’s links and I see it was introduce in C# 2.0 and has been blogged about a few times.

Basically put:

object assignable = testedObject ?? ifNullValue;

is the same as

object temp = (testedObject);

if( temp == null )  {
    temp = (ifNullValue);

assignable = temp;

Where assignable is an assignable statement, and testedObject and ifNullValue are statements that evaluate to an object.

Its been a while since I learned something new in C#, so this was a fun excursion.


About James

I am a Senior Developer/Consultant for InfoPlanIT, LLC. I previously spent over 7 years as a Product Manager for what eventually became ComponentOne, a division of GrapeCity. While there, I helped to create ActiveReports 7, GrapeCity ActiveAnalysis, and Data Dynamics Reports.


One thought on “Null Coalescing operator

  1. Ok, maybe you can help me with this then… What am I missing?

    object wtf = null;
    object huh = null;

    while (huh == null)
    huh = wtf ?? wtf;

    Seriously… what am I missing? 😛

    Posted by Luc | September 26, 2007, 5:10 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s


%d bloggers like this: