Identifiers should have correct prefix
TypeName |
IdentifiersShouldHaveCorrectPrefix |
CheckId |
CA1715 |
Category |
Microsoft.Naming |
Breaking Change |
Breaking - when fired on interfaces. Non breaking - when fired on generic type parameters. |
Cause
The name of an externally visible interface does not start with a capital 'I'.
-or-
The name of a generic type parameter on an externally visible type or method does not start with a capital 'T'.
Rule Description
By convention, the names of certain programming elements start with a specific prefix.
Interface names should start with a capital 'I' followed by another uppercase letter. This rule reports violations for interface names such as 'MyInterface' and 'IsolatedInterface'.
The first letter of a generic type parameter name must be an upper-case T. If the parameter name has two or more letters, the second letter can be any letter and it must also be uppercase. 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 required for new software libraries, and increases customer confidence that the library was developed by someone with expertise in developing managed code.
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.
Example
The following example shows an incorrectly named interface.
Imports System
Namespace Samples
Public Interface Book ' Violates this rule
ReadOnly Property Title() As String
Sub Read()
End Interface
End Namespace
using System;
namespace Samples
{
public interface Book // Violates this rule
{
string Title
{
get;
}
void Read();
}
}
using namespace System;
namespace Samples
{
public interface class Book // Violates this rule
{
property String^ Title
{
String^ get();
}
void Read();
};
}
The following example fixes the previous violation by prefixing the interface with 'I'.
Imports System
Namespace Samples
Public Interface IBook ' Fixes the violation by prefixing the interface with 'I'
ReadOnly Property Title() As String
Sub Read()
End Interface
End Namespace
using System;
namespace Samples
{
public interface IBook // Fixes the violation by prefixing the interface with 'I'
{
string Title
{
get;
}
void Read();
}
}
using namespace System;
namespace Samples
{
public interface class IBook // Fixes the violation by prefixing the interface with 'I'
{
property String^ Title
{
String^ get();
}
void Read();
};
}
The following example shows an incorrectly named generic type parameter.
Imports System
Namespace Samples
Public Class Collection(Of Item) ' Violates this rule
End Class
End Namespace
using System;
namespace Samples
{
public class Collection<Item> // Violates this rule
{
}
}
using namespace System;
namespace Samples
{
generic <typename Item> // Violates this rule
public ref class Collection
{
};
}
The following example fixes the previous violation by prefixing the generic type parameter with 'T'.
Imports System
Namespace Samples
Public Class Collection(Of TItem) ' Fixes the violation by prefixing the generic type parameter with 'T'
End Class
End Namespace
using System;
namespace Samples
{
public class Collection<TItem> // Fixes the violation by prefixing the generic type parameter with 'T'
{
}
}
using namespace System;
namespace Samples
{
generic <typename TItem> // Fixes the violation by prefixing the generic type parameter with 'T'
public ref class Collection
{
};
}