I've often written code like:
short? value = 2; short doubleIt = (value ?? 0) * 2;
Notice on the second line that the null coalescing (??) operator requires parenthesis around it before multiplying it by 2. Another options is to use GetValueOrDefault()
, as such:
short? value = 2; short doubleIt = value.GetValueOrDefault() * 2;
It's a subtle difference, but helpful at times. My primary reason for writing this short article is to get me to remember to start using it.