C# – Explicitly compare bool? with true/false to avoid compile-time errors and runtime exceptions

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.

Fortunately, there is a simple, concise way to treat nullable bools almost-like regular bools. You have to explicitly compare them with true/false. For example, to check if a nullable bool is true, you can do the the following:

if (nullableBool == true)

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

This article shows the various problems you can run into if you try to use bool? like a regular bool.

Problems when trying to use bool? exactly like a regular bool

The following is a list of possible compile-time errors and runtime exceptions you can run into when using nullable bools.

1 – Using bool? with logical operators with other variables in a conditional statement

When you try to use a bool? variable in a conditional statement with any other variable, with any logical operator, you’ll get a compile-time error like the following:

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

Example of code that results in this error:

bool? nullableBool = null; bool regularBool = true; if (nullableBool && regularBool)

You would get a similar compiler error if you tried to use a logical OR.

2 – Using bool? by itself in a conditional statement

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)

3 – When using null-conditional (?.) in a conditional statement

When you use the null-conditional 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())

This will give you the following compiler error:

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

Why? Everything after the ?. becomes nullable. Because the Any() call is after the null-conditional, it treats it like a nullable bool. In point number 2 above, we saw we cannot use nullable bools like this in a conditional statement.

4 – Using the .Value of null bool?

A 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.’

The following code results in the runtime exception above:

bool? nullableBool = null; if (nullableBool.Value)

Solution – use bool? == true

The solution is simple: explicitly compare the nullable bool with true/false.

For example, instead of this:

bool? nullableBool = null; bool regularBool = true; if (nullableBool && regularBool)

Use this:

bool? nullableBool = null; bool regularBool = true; if (nullableBool == true && regularBool)

Leave a Comment