Snipps by Patrik

How to Convert Nullable bool? to bool

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

Solution:

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

Example:

bool? nullBool = null;
bool convertedBool = nullBool ?? false; // convertedBool will be false

The null-coalescing operator ?? returns the value of its left-hand operand if it isn't null; otherwise, it evaluates the right-hand operand and returns its result. 

Comments

Leave a Comment

All fields are required. Your email address will not be published.