CA1007: Use generics where appropriate
The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.
The latest version of this topic can be found at CA1007: Use generics where appropriate.
TypeName|UseGenericsWhereAppropriate|
|CheckId|CA1007|
|Category|Microsoft.Design|
|Breaking Change|Breaking|
An externally visible method contains a reference parameter of type System.Object, and the containing assembly targets .NET Framework 2.0.
A reference parameter is a parameter that is modified by using the ref (ByRef in Visual Basic) keyword. The argument type that is supplied for a reference parameter must exactly match the reference parameter type. To use a type that is derived from the reference parameter type, the type must first be cast and assigned to a variable of the reference parameter type. Use of a generic method allows all types, subject to constraints, to be passed to the method without first casting the type to the reference parameter type.
To fix a violation of this rule, make the method generic and replace the Object parameter by using a type parameter.
Do not suppress a warning from this rule.
The following example shows a general-purpose swap routine that is implemented as both nongeneric and generic methods. Note how efficiently the strings are swapped by using the generic method compared to the nongeneric method.
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
CA1006: Do not nest generic types in member signatures
CA1004: Generic methods should provide type parameter