5

Possible Duplicate:
What is the “??” operator for?

I have recently come across the ?? operator in C#. What does this operator do and when would someone use it?

Example:

string name = nameVariable ?? string.Empty;
Community
  • 1
  • 1
brainimus
  • 10,586
  • 12
  • 42
  • 64
  • 13
    Man; if only people would document languages when they invent them. – i_am_jorf Aug 04 '10 at 18:56
  • @jeff- Go look on the C# Operators page and let me know how that goes for ya. EDIT: Apparently there are multiple versions of the C# Operators page. If you navigate to the one specifically on VS 2010 you can find it, otherwise it appears absent. – Mike M. Aug 04 '10 at 19:00
  • 2
    @Mike M: Or the VS 2005 one. Or the VS 2008 one. The null coalescing operator was introduced as part of C# 2. – Jon Skeet Aug 04 '10 at 19:05
  • @0xA3: I did search SO prior to posting. When you search for "?? operator C#" the question you referenced doesn't show up. What I didn't realized when I searched the first time is the "??" is being dropped from my search criteria. – brainimus Aug 04 '10 at 19:08
  • @jon - Yes, you win. I concede. However, if you just Google C# operators, the first result doesn't have it listed. – Mike M. Aug 04 '10 at 19:33

6 Answers6

10

The ?? operator basically means "or if it is null, blah". It is equivalent to:

string name = (nameVariable == null) ? string.Empty : nameVariable;

Which, if you're not familiar with the syntax, is basically:

string name;
if (nameVariable == null)
    name = string.Empty;
else
    name = nameVariable;
Lucas Jones
  • 19,767
  • 8
  • 75
  • 88
6

It's a null-coalescing operator It will right part if the left one is null.

The interesting fact is that you can even use it like this:

string temp = (first ?? second).Text

and it will return Text property of the 'second' variable if 'first' is null.

Giorgi
  • 30,270
  • 13
  • 89
  • 125
3

It has the catchy title of the Null Coalescing Operator. What it does is evaluate an expression and then if the expression is null it returns the right-hand operand, otherwise it returns the left-hand operand (ie. the original value).

Using your example as a basis you'd get these results:

string nameVariable = "Diplodocus";
string name = nameVariable ?? string.Empty;
// assigns name the value "Diplodocus"

And...

string nameVariable = null;
string name = nameVariable ?? string.Empty; 
// assigns name the value String.Empty;

Note you can use it with any reference or nullable type, not just strings.

Dan Diplo
  • 25,076
  • 4
  • 67
  • 89
1

It is equivalent to checking for null and setting the value to something if the first one is. Your statement above is equivalent to:

string name = nameVariable == null ? string.Empty : nameVariable;
Steve Danner
  • 21,818
  • 7
  • 41
  • 51
0

It is a null reference check if nameVariable is null it would return an empty string.

Matt
  • 2,795
  • 2
  • 29
  • 47
0

The expression

value1 ?? value2

returns value1 if value1 is a value different from null, and returns value2 if value1 equals null.

devio
  • 36,858
  • 7
  • 80
  • 143