Nullable bool as tri-state variable in C#

Petrus Theron picture Petrus Theron · Mar 16, 2011 · Viewed 26.2k times · Source

Is there any merit to using a nullable bool to store a tri-state value? For example, null == 1st state, false == 2nd state, true == 3rd state?

The overhead is probably higher than using a byte enum, but I'm curious.

Answer

Oliver picture Oliver · Mar 16, 2011

You should get a copy of Framework Design Guidelines.

There on page 177 is a chapter Choosing Between Enum and Boolean Parameters.

One of the points there is:

  • DO NOT use Booleans unless you are absolutely sure there will never be a need for more than two values.