I'm trying to do the following check:
if (result?.Pets?.Any() == false) { ... }
and if result == null
.. then it's not going inside the scope of that if
check.
I thought that result?.
will (in this case) return null
.. so is this then trying to do:
if (null == false) { ... } // result == null
and if that is the case .. then what? My first thought was something like this:
if (result?.Pets?.Any() == false ?? false) { .. }
but that is a compiler error :(
As others have explained, null does not equal false. Using the null-coalescing operator
??
to get around this should work but your syntax is incorrect. The correct syntax would be something like this:However, for readability, I would go for something like this:
Alternatively, you could do it the good old way using a null check and the short-circuiting
||
operator: