Skip to content

Latest commit

 

History

History
104 lines (71 loc) · 6.18 KB

File metadata and controls

104 lines (71 loc) · 6.18 KB
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
CA1008
EnumsShouldHaveZeroValue
CA1008
EnumsShouldHaveZeroValue
gewarren
gewarren
CSharp
VB

CA1008: Enums should have zero value

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

Cause

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.

Rule description

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.

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.

When to suppress warnings

Do not suppress a warning from this rule except for flags-attributed enumerations that have previously shipped.

Suppress a warning

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.

Configure code to analyze

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]

Additional zero-value field names

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`

Example

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":::

Related rules

See also

  • xref:System.Enum?displayProperty=fullName