Wednesday, January 02, 2008 1:14 PM Vimpyboy

Tip of the day: Double question mark

A not so common, but very usefull operator is the double question mark operator (??). This can be very usefull while working with nullable types.

Lets say you have two nullable int:

int? numOne = null;
int? numTwo = 23;

Scenario: If numOne has a value, you want it, if not you want the value from numTwo, and if both are null you want the number ten (10).

Old solution:

if (numOne != null)
    return numOne;
if (numTwo != null)
    return numTwo;

return 10;

 Or another solution with a single question mark:

return (numOne != null ? numOne : (numTwo != null ? numTwo : 10));

But with the double question mark operator we can do this:

return ((numOne ?? numTwo) ?? 10);

As you can see, the double question mark operator returns the first value that is not null.

Filed under: , ,

Comments

# re: Tip of the day: Double question mark

Wednesday, January 02, 2008 1:57 PM by some guy

for those that are interested this operator is named the coalesce operator

# Our daily link (2008-01-02)

Wednesday, January 02, 2008 5:10 PM by Trumpi's blog

I've been checking out JQuery today. It is fantastic! C# In C#, When to use String verses string

# re: Tip of the day: Double question mark

Friday, April 18, 2008 1:38 PM by johnrviner

Yes this is handy, it shortens the code...but riddle me this...is it more readable?

The answer from here is: No.

Why is readability important?

if (numOne != null)

   return numOne;

if (numTwo != null)

   return numTwo;

Can without much problem be translated into VB, Pascal, C++....or understood.  A language if it branches out too far into its own dialect becomes harder to understand and translate.  

This doesn't add any new useability to the language so then what's the point?  Its just a dialect change.

# re: Tip of the day: Double question mark

Friday, May 16, 2008 12:01 PM by deap82

Hum, how come this can't be found at msdn.com...?

# re: Tip of the day: Double question mark

Friday, May 16, 2008 4:25 PM by Mikael Söderström

I guess you can find it somewhere, it´s a really good operator.

# re: Tip of the day: Double question mark

Thursday, November 13, 2008 1:21 PM by Will Davies

It's readable if you know what it means. Arabic is readable in Iraq.

With fewer lines of code you have less room for misinterpretation.

Retrun the first !null value VS. a series of if-then-else (nested) plus all the semi-colons.

That is an easy choice for a spartan to make.

# re: Tip of the day: Double question mark

Wednesday, December 10, 2008 1:32 AM by atarikg

Thanks for information :)

# re: Tip of the day: Double question mark

Friday, March 27, 2009 4:40 PM by Joey H.

This is, indeed, found on MSDN (msdn.microsoft.com/.../ms173224.aspx).  This technique is called the Null Coalescing Operator.  As far as readability is concerned, this is simply a shorthand technique, much like lambda statements.  If you're concerned about programming for those who don't know how to read/write C#, then you're best suited to stick with traditional if/then statements, otherwise, have fun with these shorthand techniques.  Let everyone else google the meaning!

# Tip of the day: Double question mark - Mikael S??derstr??m « Redditech ‘Ground Zero’ Blog

Pingback from  Tip of the day: Double question mark - Mikael S??derstr??m « Redditech ‘Ground Zero’ Blog

Leave a Comment

(required) 
(required) 
(optional)
(required)