Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does a double question mark do in C#? [duplicate]

Possible Duplicates:
?? Null Coalescing Operator --> What does coalescing mean?
What do two question marks together mean in C#?

I couldn't find this question being asked here so I figured I would ask it. What does a double question mark do in C#?

Example:

x = y ?? z; 
like image 674
tom d Avatar asked Oct 22 '09 15:10

tom d


People also ask

What does double question mark do?

If double question marks are uses it is to emphasise something in return, usually from the shock of the previous thing said. For example, if I said: 'My dog just died' (sad, but used for example...)

What is double question mark in C sharp?

The C# double question mark operator ?? is called the null-coalescing operator. It is used to return the left hand side operand of the operator if the value is not null and the right hand side operand if the value is null.

What does '?' Mean in C programming?

Most likely the '?' is the ternary operator. Its grammar is: RESULT = (COND) ? ( STATEMEN IF TRUE) : (STATEMENT IF FALSE) It is a nice shorthand for the typical if-else statement: if (COND) { RESULT = (STATEMENT IF TRUE); } else { RESULT = (STATEMENT IF FALSE);

What is the use of question mark in C?

The question mark operator, ?:, is also found in C++. Some people call it the ternary operator because it is the only operator in C++ (and Java) that takes three operands. If you are not familiar with it, it's works like an if-else, but combines operators.


2 Answers

This is a null coalescing operator. The method above states x is assigned y's value, unless y is null, in which case it is assigned z's value.

like image 76
Alexis Abril Avatar answered Sep 23 '22 23:09

Alexis Abril


From Wikipedia:

It's the null-coalesce operator and shorthand for this:

x = (y != null ? y : z); 
like image 33
Austin Salonen Avatar answered Sep 19 '22 23:09

Austin Salonen