Поделиться через


CA1006: Do not nest generic types in member signatures

Item Value
RuleId CA1006
Category Microsoft.Design
Breaking change Breaking

Cause

An externally visible member has a signature that contains a nested type argument.

Rule description

A nested type argument is a type argument that is also a generic type. To call a member whose signature contains a nested type argument, the user must instantiate one generic type and pass this type to the constructor of a second generic type. The required procedure and syntax are complex and should be avoided.

How to fix violations

To fix a violation of this rule, change the design to remove the nested type argument.

When to suppress warnings

Do not suppress a warning from this rule. Providing generics in a syntax that is easy to understand and use reduces the time that is required to learn and increases the adoption rate of new libraries.

Example

The following example shows a method that violates the rule and the syntax that is required to call the violating method.

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: Avoid excessive parameters on generic types

CA1010: Collections should implement generic interface

CA1000: Do not declare static members on generic types

CA1002: Do not expose generic lists

CA1004: Generic methods should provide type parameter

CA1003: Use generic event handler instances

CA1007: Use generics where appropriate

See also

Generics