Is this an if else statement
Possible Duplicate:
What do two question marks together mean in C#?
I just came across the code below and not really sure what it means and can't google it because google omits the ??
int? x = 32;
int y = x ?? 5;
Is the second line some sort of if else statement, what does the ??
mean
It's called the null-coalescing operator.
If the value to the left of the ??
is null
, then use the value to the right of the ??
otherwise use the left hand value.
Expanded out:
y = x == null ? 5 : x
or
if(x == null)
y = 5
else
y = x
if(x == null)
y = 5
else
y = x
The ??
operator is used with a collection of variables and evaluates to the value of the first non-null variable. For example, consider the following code:
int? x = null;
int? y = null;
int? z = null;
y = 12;
int? a = x ?? y ?? z;
The value of a
will be 12, because y
is the first variable in the statement with a non-null value.
上一篇: 在.net中使用双重问号
下一篇: 这是一个if else语句吗?