Null-coalescing and operator precedence
Today I encountered some problem. In one application two values were to be subtracted. In the source code I've found statement:
return Value1 ?? 0 - Value2 ?? 0;
what is wrong with it? The answer is: opertor precedence! The null-coalescing (??) operator has lower precedence than subtraction operator. The correct code is of course:
return (Value1 ?? 0) - (Value2 ?? 0);