Редагувати

Поділитися через


CA1711: Identifiers should not have incorrect suffix

Property Value
Rule ID CA1711
Title Identifiers should not have incorrect suffix
Category Naming
Fix is breaking or non-breaking Breaking
Enabled by default in .NET 9 No

Cause

An identifier has an incorrect suffix.

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

Rule description

By convention, only the names of types that extend certain base types or that implement certain interfaces, or types derived from these types, should end with specific reserved suffixes. Other type names should not use these reserved suffixes.

The following table lists the reserved suffixes and the base types and interfaces with which they are associated.

Suffix Base type/Interface
Attribute System.Attribute
Collection System.Collections.ICollection

System.Collections.IEnumerable

System.Collections.Queue

System.Collections.Stack

System.Collections.Generic.ICollection<T>

System.Data.DataSet

System.Data.DataTable
Dictionary System.Collections.IDictionary

System.Collections.Generic.IDictionary<TKey,TValue>
EventArgs System.EventArgs
EventHandler An event-handler delegate
Exception System.Exception
Permission System.Security.IPermission
Queue System.Collections.Queue
Stack System.Collections.Stack
Stream System.IO.Stream

In addition, the following suffixes should not be used:

  • Delegate
  • Enum
  • Impl (use Core instead)
  • Ex or similar suffix to distinguish it from an earlier version of the same type
  • Flag or Flags for enum types

Naming conventions provide a common look for libraries that target the common language runtime. This reduces the learning curve that is required for new software libraries, and increases customer confidence that the library was developed by someone who has expertise in developing managed code. For more information, see Naming guidelines: Classes, Structs, and Interfaces.

How to fix violations

Remove the suffix from the type name.

When to suppress warnings

Do not suppress a warning from this rule unless the suffix has an unambiguous meaning in the application domain.

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

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

[*.{cs,vb}]
dotnet_diagnostic.CA1711.severity = none

For more information, see How to suppress code analysis warnings.

Configure code to analyze

Use the following options to configure which parts of your codebase to run this rule on.

You can configure these options 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

Allow suffixes

You can configure a list of allowed suffixes, with each suffix separated by the pipe character ("|"). For example, to specify that the rule should not run against Flag and Flags suffixes, add the following key-value pair to an .editorconfig file in your project:

dotnet_code_quality.ca1711.allowed_suffixes = Flag|Flags

See also