Thursday, 14 June 2012

C# Null Coalescing Operator


The C# Null Coalescing Operator (??)  is a binary operator that simplifies checking for null values. 

Eg:
X??y

While executing code, if x evaluates to null, y is returned as the value. Also remember that the
 null coalescing operator (??) is right-associative which means it is evaluated from right to left. So if you have an expression of the form x ?? y ?? z, this expression is evaluated as x?? (y?? z).


Eg:
int? a = 0;
int b = a ?? 0;

Answer b=0

int? a = 6;
int b = a ?? 0;

Answer b=6

While executing code, if a evaluates to null, b is returned as the value. 



No comments:

Post a Comment