nullable bool in if() statement - checks required?

c#, nullable

Solution

Actually I don't recommend you to treat `null` as `false`. Either use non-nullable boolean, or explicitly handle `null` value. One more option to do that:

if (foo ?? false)
{

}

Problem

I just came across this in a coworkers code. He has a nullable bool type that he compares like this: ``` //foo has no value here and evaluated to false if(foo==true) { doSomething(); } ``` Typically, the way I check a nullable boolean is something like this: ``` bool IsTrue(bool? value) { return value.HasValue && value.Value; } if(IsTrue(foo)){ doSomething() } ``` Edit: I ran through both methods and they both appear work the same way. I'm asking which one is the correct way and if the extra checks are necessary?? Now I am questioning both myself and my coworkers code. Is there any point in me doing this extra check here? Or can I safely do it the first way? Thanks (and yes I did search for this! :))

Original source