Ανάγνωση στα Αγγλικά Επεξεργασία

Κοινή χρήση μέσω


CA1712: Do not prefix enum values with type name

Property Value
Rule ID CA1712
Title Do not prefix enum values with type name
Category Naming
Fix is breaking or non-breaking Breaking
Enabled by default in .NET 9 No

Cause

An enumeration contains a member whose name starts with the type name of the enumeration.

Rule description

Names of enumeration members are not prefixed with the type name because type information is expected to be provided by development tools.

Naming conventions provide a common look for libraries that target the common language runtime. This reduces the time that is required for 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

To fix a violation of this rule, remove the type name prefix from the enumeration member.

When to suppress warnings

Do not suppress a warning from this rule.

Example

The following example shows an incorrectly named enumeration followed by the corrected version.

public enum DigitalImageMode
{
    DigitalImageModeBitmap = 0,
    DigitalImageModeGrayscale = 1,
    DigitalImageModeIndexed = 2,
    DigitalImageModeRGB = 3
}

public enum DigitalImageMode2
{
    Bitmap = 0,
    Grayscale = 1,
    Indexed = 2,
    RGB = 3
}

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.

Enum values prefix trigger

You can configure the number of enumeration values required to trigger the rule. For example, to specify that the rule is triggered if one or more the enum values starts with the enum type name, add the following key-value pair to an .editorconfig file in your project:

dotnet_code_quality.CA1712.enum_values_prefix_trigger = AnyEnumValue

Examples:

Option value Summary
dotnet_code_quality.CA1712.enum_values_prefix_trigger = AnyEnumValue The rule is triggered if any of the enum values starts with the enum type name.
dotnet_code_quality.CA1712.enum_values_prefix_trigger = AllEnumValues The rule is triggered if all of the enum values start with the enum type name.
dotnet_code_quality.CA1712.enum_values_prefix_trigger = Heuristic The rule is triggered using the default heuristic, that is, when at least 75% of the enum values start with the enum type name.

See also