Enums should have zero value
Enums should have zero value
Microsoft docsDescription
The default value of an uninitialized enumeration, just like other value types, is zero. A non-flags-attributed enumeration should define a member that has the value of zero so that the default value is a valid value of the enumeration. If appropriate, name the member 'None' (or one of the additional permitted names). Otherwise, assign zero to the most frequently used member. By default, if the value of the first enumeration member is not set in the declaration, its value is zero.
If an enumeration that has the System.FlagsAttribute applied defines a zero-valued member, its name should be 'None' (or one of the additional permitted names) to indicate that no values have been set in the enumeration. Using a zero-valued member for any other purpose is contrary to the use of the System.FlagsAttribute in that the AND and OR bitwise operators are useless with the member. This implies that only one member should be assigned the value zero. If multiple members that have the value zero occur in a flags-attributed enumeration, Enum.ToString() returns incorrect results for members that are not zero.
Cause
An enumeration without an applied System.FlagsAttribute does not define a member that has a value of zero. Or, an enumeration that has an applied System.FlagsAttribute defines a member that has a value of zero but its name is not 'None'. Or, the enumeration defines multiple, zero-valued members.
By default, this rule only looks at externally visible enumerations, but this is configurable.
How to fix violations
To fix a violation of this rule for non-flags-attributed enumerations, define a member that has the value of zero; this is a non-breaking change. For flags-attributed enumerations that define a zero-valued member, name this member 'None' and delete any other members that have a value of zero; this is a breaking change.
Example
#pragma warning disable CA1008
// The code that's violating the rule is on this line.
#pragma warning restore CA1008When to suppress
Do not suppress a warning from this rule except for flags-attributed enumerations that have previously shipped.