Nullable<T>
(or ?
) exposes a HasValue
flag to denote if a value is set or the item is null.
Also, nullable types support ==
:
if (Age == null)
The ??
is the null coalescing operator and doesn't result in a boolean expression, but a value returned:
int i = Age ?? 0;
So for your example:
if (age == null || age == 0)
Or:
if (age.GetValueOrDefault(0) == 0)
Or:
if ((age ?? 0) == 0)
Or ternary:
int i = age.HasValue ? age.Value : 0;
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…