CA1006: Do not nest generic types in member signatures

 

For the latest documentation on Visual Studio 2017, see Visual Studio 2017 Documentation.

For the latest documentation on Visual Studio 2017, see CA1006: Do not nest generic types in member signatures on docs.microsoft.com.

TypeNameDoNotNestGenericTypesInMemberSignatures
CheckIdCA1006
CategoryMicrosoft.Design
Breaking ChangeBreaking

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

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.

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

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.

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

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

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

Generics

Show: