Check if boolean is true? [closed]

Almost everyone I’ve seen expressing an opinion prefers

if (foo)
{
}

Indeed, I’ve seen many people criticize the explicit comparison, and I may even have done so myself before now. I’d say the “short” style is idiomatic.

EDIT:

Note that this doesn’t mean that line of code is always incorrect. Consider:

bool? maybeFoo = GetSomeNullableBooleanValue();
if (maybeFoo == true)
{
    ...
}

That will compile, but without the “== true” it won’t, as there’s no implicit conversion from bool? to bool.

Leave a Comment