8

Possible Duplicate:
What do two question marks together mean in C#?

I'm trying to understand what this statment does: what does "??" mean? is this som type if if-statment?

string cookieKey = "SearchDisplayType" + key ?? "";
Community
  • 1
  • 1
Troj
  • 11,781
  • 12
  • 40
  • 49
  • 4
    If one could search for "??" on SO this would qualify as a duplicate of e.g. [this](http://stackoverflow.com/questions/770186/null-coalescing-operator-what-does-coalescing-mean). :-( – Martin Hennings Sep 22 '10 at 09:11
  • 5
    1. Start C#. 2. `??` 3. PROFIT! – leppie Sep 22 '10 at 09:23
  • watch out because of operators priority http://msdn.microsoft.com/en-us/library/6a71f45d.aspx – tom3k Sep 22 '10 at 09:28
  • i ment that operator ?? is applied to whole statement : "SearchDisplayType" + key which is always not null – tom3k Sep 22 '10 at 09:32
  • Yes, you're right, it's discussed [here (for reference)](http://stackoverflow.com/questions/3259352/weird-operator-precedence-with-null-coalescing-operator). Note to self: Always use (parantheses) with the null-coalescing operator... – Martin Hennings Sep 22 '10 at 09:33

7 Answers7

14

It's the Null Coalescing operator. It means that if the first part has value then that value is returned, otherwise it returns the second part.

E.g.:

object foo = null;
object rar = "Hello";

object something = foo ?? rar;
something == "Hello"; // true

Or some actual code:

IEnumerable<Customer> customers = GetCustomers();
IList<Customer> customerList = customers as IList<Customer> ?? 
    customers.ToList();

What this example is doing is casting the customers as an IList<Customer>. If this cast results in a null, it'll call the LINQ ToList method on the customer IEnumerable.

The comparable if statement would be this:

IEnumerable<Customer> customers = GetCustomers();
IList<Customer> customersList = customers as IList<Customer>;
if (customersList == null)
{
     customersList = customers.ToList();
}

Which is a lot of code compared to doing it within a single line using the null-coalescing operator.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
djdd87
  • 67,346
  • 27
  • 156
  • 195
5

It's this. Well, not really.

Actually, it's this. And this, this, this and this, to name a few. I used almighty Google to find them, since SO has no function to search in the answers (correct?), thus making it hard to find duplicates to this kind of question. Well, for the future, use this as reference. ;-)

It's called the null-coalescing operator. It's basically the same as

int? nullableDemoInteger;
// ...
int someValue = nullableDemoInteger ?? -1;
// basically same as
int someValue = nullableDemoInteger.HasValue ? nullableDemoInteger.Value : -1;
// basically same as
int someValue;
if(nullableDemoInteger.HasValue)
    someValue = nullableDemoInteger.Value;
else
    someValue = -1;
Community
  • 1
  • 1
Martin Hennings
  • 16,418
  • 9
  • 48
  • 68
4

It's the null-coalescing operator. In this case it's roughly equivalent to:

string cookieKey;
string temp = "SearchDisplayType" + key;
if (temp == null)
    cookieKey = "";
else
    cookieKey = temp;

And, since "SearchDisplayType" + key can never be null, this is exactly equivalent to:

string cookieKey = "SearchDisplayType" + key;
LukeH
  • 263,068
  • 57
  • 365
  • 409
2

Its called null-coalescing operator. See: http://msdn.microsoft.com/en-us/library/ms173224.aspx

MrDosu
  • 3,427
  • 15
  • 18
1

This is the Null Coalescing operator which checks if the value is null and returns the value after the ?? if it is.

Peter
  • 14,221
  • 15
  • 70
  • 110
1

Its the null-coalescing operator.

This means that if key is not null, it uses the value of key, If key is null, it uses the value "".

Øyvind Bråthen
  • 59,338
  • 27
  • 124
  • 151
1

?? is the null-coalescing.

From MSDN:

The ?? operator is called the null-coalescing operator and is used to define a default value for a nullable value types as well as reference types. It returns the left-hand operand if it is not null; otherwise it returns the right operand.

Note, however, that in your case the left part of the expression cannot be null, beacuse it's a concatenation of string constant with a variable. If key is null, then "SearchDisplayType" + key evaluates to "SearchDisplayType".

I guess the intent of your statement could be implemented with:

string cookieKey = key==null ? "" : "SearchDisplayType"+key;

using this code, cookieKey is set to empty string if key is null, otherwise is set to the concatenation of "SearchDisplayType"+key

Andrea Parodi
  • 5,534
  • 27
  • 46