In your first case ( list?.Count ) the operator returns int? - a value with a null int value.
The > operator is defined for integers with a zero value, so what if int? it does not matter (equal to null), the comparison will return false .
In your second example ( a?.B ) returns bool? (because if a is null, then true and false , but null returned). And bool? cannot be used in an if because the if requires a (non-nullable) bool .
You can change this statement to:
if (a?.B ?? false)
so that he works again. Thus, the null-coalescing ( ?? ) operator returns false when the operator with the null condition ( ?. ) Returned null .
Or (as suggested by TheLethalCoder):
if (a?.B == true)
RenΓ© Vogt Jun 29 '16 at 15:31 2016-06-29 15:31
source share