Convert nullable bool? to bool

You can use Nullable{T} GetValueOrDefault() method. This will return false if null.

 bool? nullableBool = null;

 bool actualBool = nullableBool.GetValueOrDefault();

You ultimately have to decide what the null bool will represent. If null should be false, you can do this:

bool newBool = x.HasValue ? x.Value : false;

Or:

bool newBool = x.HasValue && x.Value;

Or:

bool newBool = x ?? false;

You can use the null-coalescing operator: x ?? something, where something is a boolean value that you want to use if x is null.

Example:

bool? myBool = null;
bool newBool = myBool ?? false;

newBool will be false.

Tags:

C#

Nullable