Sign in
in
   
"It is the mark of an educated mind to be able to entertain a thought without accepting it."  -Aristotle

About Me

I am a co-founder of Notches, an early stage startup currently based in NYC. We are building a free, open reviews network that anyone can participate in and anyone can build on top of. You can find out more on our official blog.

Read more about my background.

Connect with me on...

Recent Readers

Flickr Photos

 

Warning:

This article is more than 45 days old. Given the speed at which the technology world moves, this post is probably somewhat out of date. Please keep this in mind when reading the post. If this is a tutorial, please check whether you are using the same versions mentioned in the article.

Nullable Value Types and the Null Coalescing Operator

Scott recently discovered one of my favorite syntactic niceties in C# 2.0, the Null Coalescing Operator. This operator allows you to return the left side of the statement if it is not null, otherwise return what is on the right.

In other words, instead of writing code like this:

     if (o != null) 
         return o;
     else
         return defaultObj; 

Or this:

     return (o != null)? o : defaultObj;

You can simply write this:

     return o ?? defaultObj;

It can be a little disorienting when you first see it, but it definitely leads to cleaner code in my opinion.

Another (related) feature in C# is the nullable type declaration which is available thanks to the Generics support added in CLR 2.0. This effectively allows you to have three values for a boolean value ( on, off or null ) - a trick most DBAs know well.

     //declare nullable boolean value
     bool? yourFlag = null;

You can use this syntax to create This also allows you to create a nullable DateTime or int object.

     DateTime? selectedDate = null;
int? i = null;

As Ted Neward explained back in 2004, nothing has fundamentally changed under the hood to enable this syntax. 

Under the hood, nothing has changed, believe it or not; this was introduced as part of the support for generics, via the System.Nullable<X> type. Nullable<> is a generic wrapper around any value type, and introduces a boolean property "HasValue", to indicate whether the wrapper actually has a value in it, or is in fact pointing to null. The conversion between int? and Nullable<int> is purely a linguistic one, much in the same way the "conversion" between int and System.Int32 is. 

Both are very nice additions to an already elegant language.

Only published comments... Aug 08 2006, 12:37 PM by Tim

View related posts

 

Josh Einstein said:

Not entirely true. Actually, quite a bit changed in the CLR to support nullable types. Initially, they were just going to use the regular generics support to make nullables work. The problem is, nullables weren't behaving the way people expected. Stuff like:

Nullable<int> i = null;

if ( i == null ) ...

if ( i.HasValue ) ...

were not working right. That might not be the specific example, but prior to Whidbey Beta 2, there were lots of cases where you had to use HasValue instead of comparing to null. In response to customer feedback, they changed the CLR to "special case" nullables so they now work as expected in every case.

So anyway, there's a MSDN blog post floating around somewhere about it. :)

August 8, 2006 2:02 PM
   

Tim said:

Ahh, interesting, I didn't realize that. If you have the post, would love a link.

August 8, 2006 6:30 PM
 

Josh Einstein said:

August 10, 2006 10:17 AM