Search code examples
c#nullableboolean

What is the use of Nullable<bool> type?


a bool variable could hold true or false, while bool? could be null as well.

Why would we need a third value for bool ? If it is not true, what ever it is, it is == false

Can you suggest a scenario where I would fancy a bool? instead.

Thanks


Solution

  • Something can be true, false or undefined for many reasons. How would one answer "Is your third child a girl?" if one only has two children? Both true and false are incorrect. Null would be appropriate as saying that the comparison doesn't apply.