?? Operator

If you use nullable types in C# (e.g., int?, double?, boolean?, etc.) you may be interested to know that C# has a special operator for handling what to do if a value is null. It is called the null-coalescing operator. It works like this . . .

int? x = null;
int y = x ?? -1;

If x is null, then the value on the right side of the ?? will be used; otherwise, it will use the value of x to assign to y in the example above.

So, you don’t need to do things like this anymore . . .

int y = (x == null) ? x : -1;

Neat huh?