Best way to check for nullable bool in a condition expression (if …) [closed]

I think a lot of people concentrate on the fact that this value is nullable, and don’t think about what they actually want 🙂

bool? nullableBool = true;
if (nullableBool == true) { ... } // true
else { ... } // false or null

Or if you want more options…

bool? nullableBool = true;
if (nullableBool == true) { ... } // true
else if (nullableBool == false) { ... } // false
else { ... } // null

(nullableBool == true) will never return true if the bool? is null 😛

Leave a Comment