Редагувати

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


CA1715: Identifiers should have correct prefix

Property Value
Rule ID CA1715
Title Identifiers should have correct prefix
Category Naming
Fix is breaking or non-breaking Breaking - when fired on interfaces.

Non-breaking - when raised on generic type parameters.
Enabled by default in .NET 9 No

Cause

The name of an interface does not start with an uppercase 'I'.

-or-

The name of a generic type parameter on a type or method does not start with an uppercase 'T'.

By default, this rule only looks at externally visible interfaces, types, and methods, but this is configurable.

Rule description

By convention, the names of certain programming elements start with a specific prefix.

Interface names should start with an uppercase 'I' followed by another uppercase letter. This rule reports violations for interface names such as 'MyInterface' and 'IsolatedInterface'.

Generic type parameter names should start with an uppercase 'T' and optionally may be followed by another uppercase letter. This rule reports violations for generic type parameter names such as 'V' and 'Type'.

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.

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

Single-character type parameters

You can configure whether or not to exclude single-character type parameters from this rule. For example, to specify that this rule should not analyze single-character type parameters, add one of the following key-value pairs to an .editorconfig file in your project:

# Package version 2.9.0 and later
dotnet_code_quality.CA1715.exclude_single_letter_type_parameters = true

# Package version 2.6.3 and earlier
dotnet_code_quality.CA2007.allow_single_letter_type_parameters = true

Note

This rule never fires for a type parameter named T, for example, Collection<T>.

How to fix violations

Rename the identifier so that it is correctly prefixed.

When to suppress warnings

Do not suppress a warning from this rule.

Interface naming example

The following code snippet shows an incorrectly named interface:

' Violates this rule
Public Interface Book

    ReadOnly Property Title() As String

    Sub Read()

End Interface
// Violation.
public interface Book
{
    string Title
    {
        get;
    }

    void Read();
}

The following code snippet fixes the previous violation by prefixing the interface with 'I':

// Fixes the violation by prefixing the interface with 'I'.
public interface IBook
{
    string Title
    {
        get;
    }

    void Read();
}
' Fixes the violation by prefixing the interface with 'I'
Public Interface IBook

    ReadOnly Property Title() As String

    Sub Read()

End Interface

Type parameter naming example

The following code snippet shows an incorrectly named generic type parameter:

' Violates this rule
Public Class Collection(Of Item)

End Class
// Violation.
public class Collection<Item>
{
}

The following code snippet fixes the previous violation by prefixing the generic type parameter with 'T':

// Fixes the violation by prefixing the generic type parameter with 'T'.
public class Collection<TItem>
{
}
' Fixes the violation by prefixing the generic type parameter with 'T'
Public Class Collection(Of TItem)

End Class