次の方法で共有


CA1006: ジェネリック型をメンバー シグネチャ内で入れ子にしません

TypeName

DoNotNestGenericTypesInMemberSignatures

CheckId

CA1006

カテゴリ

Microsoft.Design

互換性に影響する変更点

あり

原因

外部から参照可能なメンバーが、入れ子にされた型引数を含むシグネチャを持っています。

規則の説明

入れ子にされた型引数は、ジェネリック型の型引数でもあります。 入れ子にされた型引数を含むシグネチャを持つメンバーを呼び出すには、ユーザーが 1 つのジェネリック型をインスタンス化し、別のジェネリック型のコンストラクターにこの型を渡す必要があります。 複雑な手順と構文が必要となるため、これは避けるようにしてください。

違反の修正方法

この規則違反を修正するには、デザインを変更して、入れ子にされた型引数を削除します。

警告を抑制する状況

この規則による警告は抑制しないでください。 理解しやすく使いやすい構文でジェネリック型を指定することで、習得に必要な時間が短縮され、新しいライブラリの採用率が向上します。

使用例

この規則に違反するメソッドと、違反するメソッドを呼び出すために必要な構文を次の例に示します。

Imports System
Imports System.Collections.Generic

Namespace DesignLibrary

   Public Class IntegerCollections

      Sub NonNestedCollection(collection As ICollection(Of Integer))

         For Each I As Integer In DirectCast( _ 
            collection, IEnumerable(Of Integer))

            Console.WriteLine(I)

         Next 

      End Sub

      ' This method violates the rule.
      Sub NestedCollection( _ 
         outerCollection As ICollection(Of ICollection(Of Integer)))

         For Each innerCollection As ICollection(Of Integer) In _ 
            DirectCast(outerCollection, _ 
                       IEnumerable(Of ICollection(Of Integer)))

            For Each I As Integer In _ 
               DirectCast(innerCollection, IEnumerable(Of Integer))

               Console.WriteLine(I)

            Next

         Next

      End Sub

   End Class

   Class Test

      Shared Sub Main()

         Dim collections As New IntegerCollections()

         Dim integerListA As New List(Of Integer)()
         integerListA.Add(1)
         integerListA.Add(2)
         integerListA.Add(3)

         collections.NonNestedCollection(integerListA)

         Dim integerListB As New List(Of Integer)()
         integerListB.Add(4)
         integerListB.Add(5)
         integerListB.Add(6)

         Dim integerListC As New List(Of Integer)()
         integerListC.Add(7)
         integerListC.Add(8)
         integerListC.Add(9)

         Dim nestedIntegerLists As New List(Of ICollection(Of Integer))()
         nestedIntegerLists.Add(integerListA)
         nestedIntegerLists.Add(integerListB)
         nestedIntegerLists.Add(integerListC)

         collections.NestedCollection(nestedIntegerLists)

      End Sub

   End Class

End Namespace
using System;
using System.Collections.Generic;

namespace DesignLibrary
{
   public class IntegerCollections
   {
      public void NotNestedCollection(ICollection<int> collection)
      {
         foreach(int i in collection)
         {
            Console.WriteLine(i);
         }
      }

      // This method violates the rule.
      public void NestedCollection(
         ICollection<ICollection<int>> outerCollection)
      {
         foreach(ICollection<int> innerCollection in outerCollection)
         {
            foreach(int i in innerCollection)
            {
               Console.WriteLine(i);
            }
         }
      }
   }

   class Test
   {
      static void Main()
      {
         IntegerCollections collections = new IntegerCollections();

         List<int> integerListA = new List<int>();
         integerListA.Add(1);
         integerListA.Add(2);
         integerListA.Add(3);

         collections.NotNestedCollection(integerListA);

         List<int> integerListB = new List<int>();
         integerListB.Add(4);
         integerListB.Add(5);
         integerListB.Add(6);

         List<int> integerListC = new List<int>();
         integerListC.Add(7);
         integerListC.Add(8);
         integerListC.Add(9);

         List<ICollection<int>> nestedIntegerLists = 
            new List<ICollection<int>>();
         nestedIntegerLists.Add(integerListA);
         nestedIntegerLists.Add(integerListB);
         nestedIntegerLists.Add(integerListC);

         collections.NestedCollection(nestedIntegerLists);
      }
   }
}

関連規則

CA1005: ジェネリック型でパラメーターを使用しすぎないでください

CA1010: コレクションは、ジェネリック インターフェイスを実装しなければなりません

CA1000: ジェネリック型の静的メンバーを宣言しません

CA1002: ジェネリック リストを公開しません

CA1004: ジェネリック メソッドは型パラメーターを指定しなければなりません

CA1003: 汎用イベント ハンドラーのインスタンスを使用します

CA1007: 適切な場所にジェネリックを使用します

参照

参照

ジェネリック (C# プログラミング ガイド)