I've been stung by this a few times in the last month.
The ?? (Null coalescing operator) has a really low precedence in C#. It's well below the arithmetic operators. In fact, it's second from the bottom according to MSDN. For some reason, I expected it to be quite high.
Take this code as an example.
int? x = 10;
int? y = 10;
Console.WriteLine(x ?? y + 1);
Previously, I'd expect this to print 11. It actually prints 10 (since x ?? y + 1 is equivalent to x ?? (y + 1)).
0 comments:
Post a Comment