Please click this logo to help me get on their beta program:

Xobni outlook add-in for your inbox








15/6/2005

The ?? operator (C# 2.0)

Filed under: General, Programming, .NET — Oliver Sturm @ 12:18 pm - 3 years, 1 month ago

A while ago, I read a tiny note about this in somebody’s blog and I want to start off by apologizing to that somebody because for the life of me I can’t figure out who it might have been - so no reference here. Sorry about that. Anyway, I followed up on the original information and I thought it couldn’t hurt to pull peoples’ attention to this a bit more.

So, the ?? operator: It’s an extension in C# 2.0 (find the specs here, this operator is explained on page 14, then end of chapter 19.5) and they call it a null coalescing operator. Here’s a conditional expression, in three different forms, but with the same meaning:

MyClass anObject;
...
// Variant 1: Using a full if/else clause
MyClass anotherObject;
if (anObject != null)
  anotherObject = anObject;
else
  anotherObject = new MyClass();
	
// Variant 2: Using the ?/: conditional operator
MyClass anotherObject = anObject != null ? anObject : new MyClass();
	
// Variant 3: Using the ?? operator
MyClass anotherObject = anObject ?? new MyClass();

So this should really make the purpose of the operator quite clear: check something for null, return the same something if it’s not null, the given alternative value otherwise.

This functionality is slightly extended where it comes to nullable types. Normally, if both parameters to the ?? operator are nullable types, the return type will be the same nullable type. This doesn’t differ from the behaviour with any other type. But if only the first parameter is a nullable type while the alternative value is not nullable, the result type will be the non-nullable “base” type and the Value property of the nullable will be evaluated if necessary. So this is all valid code:

int? foo = null;
int bar = foo ?? 42;
// bar is now 42
	
foo = 52;
bar = foo ?? 42;
// bar is now 52 because foo.Value has been evaluated
	
int? nbar = foo ?? 42;
// nbar is now 52, but it's also a nullable type because the int has
// been converted back to an int? immediately

4 Comments »

  1. Nice.

    Comment by Marcus Mac Innes — 15/6/2005 @ 12:56 pm - 3 years, 1 month ago

  2. very usefull operator … thanks for the tips

    Comment by Mihir Solanki — 17/6/2005 @ 11:12 am - 3 years, 1 month ago

  3. It could have been a good thing if the ?? operator worked with dbnull values returned from a database. These values are ussually checked prior to assigning to some variable

    For example
    If only i could do this

    BookID = (int?)dRow["bookid"] ?? null;

    instead of this

    if (dRow["bookid"] != DBNull.Value)
    { BookID = (int?)dRow["bookid"]; }
    else
    { BookID = null; }

    I ‘ll put it on my next wishlist

    Comment by Vin K — 14/8/2007 @ 7:46 am - 11 months, 2 weeks ago

  4. Vin K, that code can be written as the following :

    BookId = dRow["BookId"] as int?;

    Comment by Anon — 7/4/2008 @ 11:03 am - 3 months, 2 weeks ago

RSS feed for comments on this post. TrackBack URI

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>


Powered by WordPress
© Copyright 2005-2008 Oliver Sturm