C#idiomatic C#
ca1715fundamentals/code-analysis/quality-rules

Code analysis

CA1715: Identifiers should have correct prefix

Learn about code analysis rule CA1715: Identifiers should have correct prefix

CA1715 Naming enabled by default in .NET 10: No
Fix is breaking or non-breaking Breaking - when fired on interfaces.Non-breaking - when raised on generic type parameters.Applicable languages C# and Visual Basic

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 they apply to, or for all rules in this category (Naming) that they apply 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, by setting the api_surface option. 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:

ini
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:

ini
# 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

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:

sample · vbdocs/fundamentals/code-analysis/quality-rules/snippets/vb/all-rules/ca1715-identifiers-should-have-correct-prefix_1.vbsnippet1

vb
Imports System

Namespace ca1715
    '<snippet1>
    ' Violates this rule
    Public Interface Book

        ReadOnly Property Title() As String

        Sub Read()

    End Interface
    '</snippet1>

    '<snippet2>
    ' Fixes the violation by prefixing the interface with 'I'
    Public Interface IBook

        ReadOnly Property Title() As String

        Sub Read()

    End Interface
    '</snippet2>

    '<snippet3>
    ' Violates this rule
    Public Class Collection(Of Item)

    End Class
    '</snippet3>
End Namespace

Namespace ca1715_2
    '<snippet4>
    ' Fixes the violation by prefixing the generic type parameter with 'T'
    Public Class Collection(Of TItem)

    End Class
    '</snippet4>

End Namespace

sample · csharpdocs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca1715.cssnippet1

csharp
namespace ca1715
{
    //<snippet1>
    // Violation.
    public interface Book
    {
        string Title
        {
            get;
        }

        void Read();
    }
    //</snippet1>

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

        void Read();
    }
    //</snippet2>

    //<snippet3>
    // Violation.
    public class Collection<Item>
    {
    }
    //</snippet3>
}

namespace ca1715_2
{
    //<snippet4>
    // Fixes the violation by prefixing the generic type parameter with 'T'.
    public class Collection<TItem>
    {
    }
    //</snippet4>
}

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

sample · csharpdocs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca1715.cssnippet2

csharp
namespace ca1715
{
    //<snippet1>
    // Violation.
    public interface Book
    {
        string Title
        {
            get;
        }

        void Read();
    }
    //</snippet1>

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

        void Read();
    }
    //</snippet2>

    //<snippet3>
    // Violation.
    public class Collection<Item>
    {
    }
    //</snippet3>
}

namespace ca1715_2
{
    //<snippet4>
    // Fixes the violation by prefixing the generic type parameter with 'T'.
    public class Collection<TItem>
    {
    }
    //</snippet4>
}

sample · vbdocs/fundamentals/code-analysis/quality-rules/snippets/vb/all-rules/ca1715-identifiers-should-have-correct-prefix_1.vbsnippet2

vb
Imports System

Namespace ca1715
    '<snippet1>
    ' Violates this rule
    Public Interface Book

        ReadOnly Property Title() As String

        Sub Read()

    End Interface
    '</snippet1>

    '<snippet2>
    ' Fixes the violation by prefixing the interface with 'I'
    Public Interface IBook

        ReadOnly Property Title() As String

        Sub Read()

    End Interface
    '</snippet2>

    '<snippet3>
    ' Violates this rule
    Public Class Collection(Of Item)

    End Class
    '</snippet3>
End Namespace

Namespace ca1715_2
    '<snippet4>
    ' Fixes the violation by prefixing the generic type parameter with 'T'
    Public Class Collection(Of TItem)

    End Class
    '</snippet4>

End Namespace

Type parameter naming example

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

sample · vbdocs/fundamentals/code-analysis/quality-rules/snippets/vb/all-rules/ca1715-identifiers-should-have-correct-prefix_1.vbsnippet3

vb
Imports System

Namespace ca1715
    '<snippet1>
    ' Violates this rule
    Public Interface Book

        ReadOnly Property Title() As String

        Sub Read()

    End Interface
    '</snippet1>

    '<snippet2>
    ' Fixes the violation by prefixing the interface with 'I'
    Public Interface IBook

        ReadOnly Property Title() As String

        Sub Read()

    End Interface
    '</snippet2>

    '<snippet3>
    ' Violates this rule
    Public Class Collection(Of Item)

    End Class
    '</snippet3>
End Namespace

Namespace ca1715_2
    '<snippet4>
    ' Fixes the violation by prefixing the generic type parameter with 'T'
    Public Class Collection(Of TItem)

    End Class
    '</snippet4>

End Namespace

sample · csharpdocs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca1715.cssnippet3

csharp
namespace ca1715
{
    //<snippet1>
    // Violation.
    public interface Book
    {
        string Title
        {
            get;
        }

        void Read();
    }
    //</snippet1>

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

        void Read();
    }
    //</snippet2>

    //<snippet3>
    // Violation.
    public class Collection<Item>
    {
    }
    //</snippet3>
}

namespace ca1715_2
{
    //<snippet4>
    // Fixes the violation by prefixing the generic type parameter with 'T'.
    public class Collection<TItem>
    {
    }
    //</snippet4>
}

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

sample · csharpdocs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca1715.cssnippet4

csharp
namespace ca1715
{
    //<snippet1>
    // Violation.
    public interface Book
    {
        string Title
        {
            get;
        }

        void Read();
    }
    //</snippet1>

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

        void Read();
    }
    //</snippet2>

    //<snippet3>
    // Violation.
    public class Collection<Item>
    {
    }
    //</snippet3>
}

namespace ca1715_2
{
    //<snippet4>
    // Fixes the violation by prefixing the generic type parameter with 'T'.
    public class Collection<TItem>
    {
    }
    //</snippet4>
}

sample · vbdocs/fundamentals/code-analysis/quality-rules/snippets/vb/all-rules/ca1715-identifiers-should-have-correct-prefix_1.vbsnippet4

vb
Imports System

Namespace ca1715
    '<snippet1>
    ' Violates this rule
    Public Interface Book

        ReadOnly Property Title() As String

        Sub Read()

    End Interface
    '</snippet1>

    '<snippet2>
    ' Fixes the violation by prefixing the interface with 'I'
    Public Interface IBook

        ReadOnly Property Title() As String

        Sub Read()

    End Interface
    '</snippet2>

    '<snippet3>
    ' Violates this rule
    Public Class Collection(Of Item)

    End Class
    '</snippet3>
End Namespace

Namespace ca1715_2
    '<snippet4>
    ' Fixes the violation by prefixing the generic type parameter with 'T'
    Public Class Collection(Of TItem)

    End Class
    '</snippet4>

End Namespace