bool? nullableVar;
if(nullableVar)
gives me an error but
if(nullableVar==true)
evaluates fine.
Not sure I follow on why that is given that if the bool wasn't nullable would evaluate fine?
The condition expression of an if has to be implicitly convertible to bool. Nullable<bool> is not convertible to bool, but the second expression is already of type bool, so it's okay. Looking at the IL, I believe the second expression is really being compiled to:
if (nullableVar.GetValueOrDefault())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With