C# – Cannot implicitly convert type bool


I am trying to convert my nullable bool value and I am getting this error.

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

For example:

public virtual bool? MyBool

if (!MyBool){}

Best Solution

As the error states, you can't use a bool? in a conditional. (What would happen if it's null?)

Instead, you can write if (MyBool != true) or if (MyBool == false), depending on whether you want to include null. (and you should add a comment explaining that)