Do not nest generic types in member signatures
.NET Framework 2.0
| TypeName | DoNotNestGenericTypesInMemberSignatures |
| CheckId | CA1006 |
| Category | Microsoft.Design |
| Breaking Change | Breaking |
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 is complex and should be avoided.
The following example shows a method that violates the rule and the syntax 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); } } }