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# 4.0 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.