Share via


CA1715:标识符应具有正确的前缀

类型名

IdentifiersShouldHaveCorrectPrefix

CheckId

CA1715

类别

Microsoft.Naming

是否重大更改

是 - 如果对接口引发。

无间断 - 如果是针对泛型类型参数引发的。

原因

外部可见的接口的名称不以大写的“I”开头。

- 或 -

外部可见的类型或方法上的泛型类型参数的名称不以大写的“T”开头。

规则说明

按照约定,某些编程元素的名称以特定前缀开头。

接口名称应以大写字母“I”开头,后跟另一个大写字母。 该规则报告诸如“MyInterface”和“IsolatedInterface”的接口名称冲突。

泛型类型参数名称应以大写字母“T”开头,后面可跟另一个大写字母。 此规则报告泛型类型参数名称的冲突,例如“V”和“Type”。

命名约定为所有针对公共语言运行时的库提供了通用的外观。 这提高了学习新软件库的效率,并使客户进一步认为该软件库是由某位具有开发托管代码专门技术的人员所开发。

如何解决冲突

重命名标识符,以便它使用正确的前缀。

何时禁止显示警告

不要禁止显示此规则发出的警告。

示例

下面的示例显示了一个未正确命名的接口。

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();
    };
}

下面的示例通过在此接口前添加前缀“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();
    };
}

下面的示例显示了一个未正确命名的泛型类型参数。

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
    {

    };
}

下面的示例通过在此泛型类型参数前添加前缀“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
    {

    };
}

相关规则

CA1722:标识符应采用正确的前缀