SCA1022: Container Used as an Untyped Collection
Do not use a container of non-copyable values as an untyped collection.
Example
// list is a List<LockGuard>
IEnumerable untyped = list; // SCA1022: hands out boxed copies
IReadOnlyList<LockGuard> typed = list; // OK: the guard type is still visible
How to Fix
Keep the element type visible, for example with a generic interface such as IReadOnlyList<LockGuard>.
Details
A container is used as Array or a non-generic collection interface, which hand out boxed copies of the elements. That can happen by:
- a conversion, including one that variance makes of a type argument;
- an inherited member;
- an override with a wider return type;
- a method group or a delegate that is converted to a delegate;
- the variable of a
foreach.
See Also
These articles explain the rule behind this warning:
- Containers and Conversions - conversions, also those with nothing written.
- Variance and Erasable Types - conversions that variance makes, and erasable types.
Didn't add this analyzer yourself? See Why Am I Seeing This?