Tip of the day: Double question mark
A not so common, but very usefull operator is the double question mark operator (??). This can be very usefull while working with nullable types.
Lets say you have two nullable int:
int? numOne = null;
int? numTwo = 23;
Scenario: If numOne has a value, you want it, if not you want the value from numTwo, and if both are null you want the number ten (10).
Old solution:
if (numOne != null)
return numOne;
if (numTwo != null)
return numTwo;
return 10;
Or another solution with a single question mark:
return (numOne != null ? numOne : (numTwo != null ? numTwo : 10));
But with the double question mark operator we can do this:
return ((numOne ?? numTwo) ?? 10);
As you can see, the double question mark operator returns the first value that is not null.