C# – Check if a nullable bool is true

You can’t use nullable bools (bool?) exactly like regular bools, because they aren’t the same thing. When you try to use them like regular bools, you run into compiler errors and runtime exceptions. Instead, you have to explicitly compare the nullable bool with true/false.

Here’s an example of checking if a nullable bool is true in an if statement:

if (nullableBool == true)
Code language: C# (cs)

This may look odd, but it’s simpler and more concise than the alternatives.

In this article, I’ll show the various errors/exceptions you can run into if you try to use bool? like a regular bool. The solution is the same in all cases: explicitly compare the nullable bool with true.

Problem 1 – Operator ‘&&’ cannot be applied to operands of type ‘bool?’ and ‘bool’

When you try to use a nullable bool in a conditional statement with another variable, you’ll get a compile-time error like this:

Operator ‘&&’ cannot be applied to operands of type ‘bool?’ and ‘bool’

Here’s an example of code that results in this error:

bool? nullableBool = null;
bool regularBool = true;

if (nullableBool && regularBool)
Code language: C# (cs)

Instead, explicitly compare the nullable bool with true:

bool? nullableBool = null;
bool regularBool = true;

if (nullableBool == true && regularBool)
Code language: C# (cs)

Problem 2 – Cannot implicitly convert type ‘bool?’ to ‘bool’

You get a different error if you try to use a bool? in a conditional statement by itself.

Cannot implicitly convert type ‘bool?’ to ‘bool’. An explicit conversion exists (are you missing a cast?)

The following shows code that would result in that compiler error:

bool? nullableBool = null;

if (nullableBool)
Code language: C# (cs)

Instead, explicitly compare the nullable bool with true:

bool? nullableBool = null;

if (nullableBool == true)
Code language: C# (cs)

Problem 3 – Errors when using null-conditional (?.) with bools

When you use the null-conditional operator (?.) in a call chain, it tries to use bool results as if they were nullable bool results. So if you’re checking the result in a conditional statement, you’ll get one of the previously mentioned compiler errors.

For example, consider the following code that uses the null-conditional operator:

Person person = new Person();

if (person.Pets?.Any())
Code language: C# (cs)

This gives you the following compiler error:

Cannot implicitly convert type ‘bool?’ to ‘bool’. An explicit conversion exists (are you missing a cast?)

The null-conditional operator makes everything after it nullable. So in this example, when Any() returns a bool, it’s treated like a nullable bool. You’ll have to explicitly compare this nullable bool with true:

Person person = new Person();

if (person.Pets?.Any() == true)
Code language: C# (cs)

Problem 4 – InvalidOperationException: ‘Nullable object must have a value.’

A nullable bool has three states: null, true, and false. If the value is not explicitly set, then it’s null. If you try to use .Value, you’ll get this runtime exception:

System.InvalidOperationException: ‘Nullable object must have a value.’

Here’s an example of code that results in this exception:

bool? nullableBool = null;

if (nullableBool.Value)
Code language: C# (cs)

Instead of using the Value property, explicitly compare the nullable bool with true:

bool? nullableBool = null;

if (nullableBool == true)
Code language: C# (cs)

Leave a Comment