I noticed that some enumerations have “None” as a enumeration member.
For example what I mean
enum Mode
{
Mode1 = 1,
Mode2 = 2,
Mode3 = 3,
None = 4
}
Why do they use it ? In what cases solution with a none member is more preferable (less preferable) ?
Logically
Nonecould be a valid choice (depends on the meaning of your enumeration) -> could have a separate branch in a switch case (arguably – not always aNoneoption makes sence)Regarding having a
Noneoption an aNullable<Mode>variable: I would go forNonefor consistency: if you have 3 valid options andNoneis one of them, why treat it differently?And if you choose or not to have a
Noneoption you should always have a enum value mapped as 0 (the default option). The link provided by Hans Kesting makes a good point about having a value mapped as 0:The default value of an un-initialized enumeration, like other value types, is zero. A non-flags attributed enumeration should define a member with the value of zero so that the default value is a valid value of the enumeration. If appropriate, name the member ‘None’. Otherwise, assign zero to the most commonly used member. Note that if the value of the first enumeration member is not set in the declaration, its value is zero by default.