CA1716: Identifiers should not match keywords

Property Value
Rule ID CA1716
Title Identifiers should not match keywords
Category Naming
Fix is breaking or non-breaking Breaking
Enabled by default in .NET 9 No

Cause

The name of a namespace, type, or virtual or interface member matches a reserved keyword in a programming language.

By default, this rule only looks at externally visible namespaces, types, and members, but you can configure visibility and symbol kinds.

Rule description

Identifiers for namespaces, types, and virtual and interface members should not match keywords that are defined by languages that target the common language runtime. Depending on the language that is used and the keyword, compiler errors and ambiguities can make the library difficult to use.

This rule checks against keywords in the following languages:

  • Visual Basic
  • C#
  • C++/CLI

Case-insensitive comparison is used for Visual Basic keywords, and case-sensitive comparison is used for the other languages.

How to fix violations

Select a name that does not appear in the list of keywords.

When to suppress warnings

You can suppress a warning from this rule if you're sure that the identifier won't confuse users of the API, and that the library is usable in all available languages in .NET.

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

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

[*.{cs,vb}]
dotnet_diagnostic.CA1716.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

Analyzed symbol kinds

You can configure the kinds of symbols that will be analyzed by this rule. The allowable values are:

  • Namespace
  • NamedType
  • Method
  • Property
  • Event
  • Parameter

Separate multiple values with a comma (,). The default value includes all of the symbol kinds in the previous list.

dotnet_code_quality.CA1716.analyzed_symbol_kinds = Namespace, NamedType, Method, Property, Event