C# – Convert nullable bool? to bool

cnullable

How do you convert a nullable bool? to bool in C#?

I have tried x.Value or x.HasValue

Best Answer

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;