Do not expose generic lists
Do not expose generic lists
Microsoft docsDescription
System.Collections.Generic.List1 is a generic collection that's designed for performance and not inheritance. System.Collections.Generic.List1 does not contain virtual members that make it easier to change the behavior of an inherited class. The following generic collections are designed for inheritance and should be exposed instead of System.Collections.Generic.List`1.
- System.Collections.ObjectModel.Collection`1
- System.Collections.ObjectModel.ReadOnlyCollection`1
- System.Collections.ObjectModel.KeyedCollection`2
- System.Collections.Generic.IList`1
- System.Collections.Generic.ICollection`1
Cause
A type contains an externally visible member that is a System.Collections.Generic.List1 type, returns a System.Collections.Generic.List1 type, or whose signature includes a System.Collections.Generic.List`1 parameter.
By default, this rule only looks at externally visible types, but this is configurable.
How to fix violations
To fix a violation of this rule, change the System.Collections.Generic.List`1 type to one of the generic collections that's designed for inheritance.
Example
#pragma warning disable CA1002
// The code that's violating the rule is on this line.
#pragma warning restore CA1002When to suppress
Do not suppress a warning from this rule unless the assembly that raises this warning is not meant to be a reusable library. For example, it would be safe to suppress this warning in a performance-tuned application where a performance benefit was gained from the use of generic lists.