แก้ไข

แชร์ผ่าน


CA1717: Only FlagsAttribute enums should have plural names

Property Value
Rule ID CA1717
Title Only FlagsAttribute enums should have plural names
Category Naming
Fix is breaking or non-breaking Breaking
Enabled by default in .NET 9 No

Cause

The name of an enumeration ends in a plural word and the enumeration is not marked with the System.FlagsAttribute attribute.

By default, this rule only looks at externally visible enumerations, but this is configurable.

Rule description

Naming conventions dictate that a plural name for an enumeration indicates that more than one value of the enumeration can be specified simultaneously. The FlagsAttribute tells compilers that the enumeration should be treated as a bit field that enables bitwise operations on the enumeration.

If only one value of an enumeration can be specified at a time, the name of the enumeration should be a singular word. For example, an enumeration that defines the days of the week might be intended for use in an application where you can specify multiple days. This enumeration should have the FlagsAttribute and could be called 'Days'. A similar enumeration that allows only a single day to be specified would not have the attribute, and could be called 'Day'.

Naming conventions provide a common look for libraries that target the common language runtime. This reduces the time that is required to learn a new software library, and increases customer confidence that the library was developed by someone who has expertise in developing managed code.

How to fix violations

Make the name of the enumeration a singular word or add the FlagsAttribute.

When to suppress warnings

It is safe to suppress a warning from the rule if the name ends in a singular word.

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 CA1717
// The code that's violating the rule is on this line.
#pragma warning restore CA1717

To disable the rule for a file, folder, or project, set its severity to none in the configuration file.

[*.{cs,vb}]
dotnet_diagnostic.CA1717.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 (Naming) that it applies to. For more information, see Code quality rule configuration options.

Include specific API surfaces

You can configure which parts of your codebase to run this rule on, based on their accessibility. For example, to specify that the rule should run only against the non-public API surface, add the following key-value pair to an .editorconfig file in your project:

dotnet_code_quality.CAXXXX.api_surface = private, internal

See also