Best way to check for nullable bool in a condition expression (if ...)

Posted by FireSnake on Stack Overflow See other posts from Stack Overflow or by FireSnake
Published on 2010-04-20T09:26:13Z Indexed on 2010/04/20 9:43 UTC
Read the original article Hit count: 192

Filed under:
|
|

I was wondering what was the most clean and understandable syntax for doing condition checks on nullable bools.

Is the following good or bad coding style? Is there a way to express the condition better/more cleanly?

bool? nullableBool = true;
if (nullableBool ?? false) { ... }
else { ... }

especially the if (nullableBool ?? false) part. I don't like the if (x.HasValue && x.Value) style ...

(not sure whether the question has been asked before ... couldn't find something similar with the search)

© Stack Overflow or respective owner

Related posts about c#

Related posts about nullable-types