次の方法で共有


CA1715: 識別子は正しいプレフィックスを含んでいなければなりません

TypeName

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

インターフェイス名の先頭に "T" を付けることによって上記の違反を修正するコード例を次に示します。

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: 識別子は不適切なプレフィックスを含むことはできません。