Lifted Operators

For nullable value types, the relational operators are also lifted. Comparison of a non-null value to null always results in false, except when inequality is checked:

int? a = 5;int? b = null;bool gt = a >  b; // falsebool le = a <= b; // falsebool eq = a == b; // falsebool ne = a != b; // true

Nullable logic can be surprising. For example, if you thought the result of >= and < would always be exact opposites when applied to the same operands, you can clearly see this doesn’t hold any longer when null is in play.

Get C# 5.0 Unleashed now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.