title | description | ms.date | f1_keywords | helpviewer_keywords | author | ms.author | dev_langs | ||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
CA1008: Enums should have zero value (code analysis) |
Learn about code analysis rule CA1008: Enums should have zero value |
03/11/2019 |
|
|
gewarren |
gewarren |
|
Property | Value |
---|---|
Rule ID | CA1008 |
Title | Enums should have zero value |
Category | Design |
Fix is breaking or non-breaking | Non-breaking - When you're prompted to add a None value to a non-flag enumeration. Breaking - When you're prompted to rename or remove any enumeration values. |
Enabled by default in .NET 8 | No |
An enumeration without an applied xref:System.FlagsAttribute?displayProperty=fullName does not define a member that has a value of zero. Or, an enumeration that has an applied xref: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.
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 xref: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 xref: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.
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.
Do not suppress a warning from this rule except for flags-attributed enumerations that have previously shipped.
If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.
#pragma warning disable CA1008
// The code that's violating the rule is on this line.
#pragma warning restore CA1008
To disable the rule for a file, folder, or project, set its severity to none
in the configuration file.
[*.{cs,vb}]
dotnet_diagnostic.CA1008.severity = none
For more information, see How to suppress code analysis warnings.
Use the following option to configure which parts of your codebase to run this rule on.
You can configure this option for just this rule, for all rules it applies to, or for all rules in this category (Design) that it applies to. For more information, see Code quality rule configuration options.
[!INCLUDEapi-surface]
In .NET 7 and later versions, you can configure other allowable names for a zero-value enumeration field, besides None
. Separate multiple names by the |
character. The following table shows some examples.
Option value | Summary |
---|---|
dotnet_code_quality.CA1008.additional_enum_none_names = Never |
Allows both None and Never |
`dotnet_code_quality.CA1008.additional_enum_none_names = Never | Nothing` |
The following example shows two enumerations that satisfy the rule and an enumeration, BadTraceOptions
, that violates the rule.
:::code language="csharp" source="snippets/csharp/all-rules/ca1008.cs":::
:::code language="vb" source="snippets/vb/all-rules/ca1008-enums-should-have-zero-value_1.vb":::
- CA2217: Do not mark enums with FlagsAttribute
- CA1700: Do not name enum values 'Reserved'
- CA1712: Do not prefix enum values with type name
- CA1028: Enum storage should be Int32
- CA1027: Mark enums with FlagsAttribute
- xref:System.Enum?displayProperty=fullName