Why do some people use No as an enumeration member?

I noticed that some enums have "None" as an enumeration element.

For example, what do I mean

enum Mode { Mode1 = 1, Mode2 = 2, Mode3 = 3, None = 4 } 

Why do they use it? When is a single-member solution (less preferred) more preferred?

+6
c #
source share
3 answers

Logically, None may be a valid choice (depends on the value of your enumeration) → may have a separate branch in the case of a switch (perhaps the None parameter does not always make sense)

Regarding the presence of the option None a Nullable<Mode> variable: I would go for None for consistency: if you have 3 valid parameters, and None is one of them, why treat it differently?

And if you choose or don't want to have the None parameter, you should always have an enumeration value displayed as 0 (the default option). The link provided by Hans Kesting gives a good idea that the value is displayed as 0:

The default value for an uninitialized enumeration, like other value types, is zero. A non-flagged enumeration must specify a member with a value of zero, so the default value is a valid enumeration value. If necessary, enter the name "No". Otherwise, assign zero to the most commonly used item. Please note that if the value of the first enumeration element is not specified in the declaration, by default it is equal to zero.

+4
source share

Nothing is important for [Flags] enumerations and should have a value of 0. Otherwise ... is doubtful. A Nullable<Mode> will also suffice. But None may be required for their serialization or ORM levels (it may appear in the expected line / int -value, etc.). Or it will just simplify the API.

+11
source share

This is required by the rule of code analysis, see http://msdn.microsoft.com/en-us/library/ms182149(VS.100).aspx for an official reason.

+4
source share

All Articles