Equality operators handle nulls just like reference types do.
Two null values are equal:
Console.WriteLine ( null == null); // True Console.WriteLine ((bool?)null == (bool?)null); // True
If exactly one operand is null, the operands are unequal.
If both operands are non-null, their Values are compared.
Comparing a null value to either a null or a non-null value returns false:
bool b = x < y; // Translation: bool b = (x.HasValue && y.HasValue) ? (x.Value < y.Value) : false;
These operators return null when any of the operands are null.
int? c = x + y; // Translation:
int? c = (x.HasValue && y.HasValue) ? (int?) (x.Value + y.Value) : null;
You can mix and match nullable and non-nullable types and there is an implicit conversion from T to T?:
int? a = null;
int b = 2;
int? c = a + b; // c is null - equivalent to a + (int?)b