CA1007: Use generics where appropriate
Note
This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here
Item | Value |
---|---|
TypeName | UseGenericsWhereAppropriate |
CheckId | CA1007 |
Category | Microsoft.Design |
Breaking Change | Breaking |
Cause
An externally visible method contains a reference parameter of type System.Object, and the containing assembly targets .NET Framework 2.0.
Rule Description
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.
How to Fix Violations
To fix a violation of this rule, make the method generic and replace the Object parameter by using a type parameter.
When to Suppress Warnings
Do not suppress a warning from this rule.
Example
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.
using System;
namespace DesignLibrary
{
public sealed class ReferenceParameters
{
private ReferenceParameters(){}
// This method violates the rule.
public static void Swap(ref object object1, ref object object2)
{
object temp = object1;
object1 = object2;
object2 = temp;
}
// This method satifies the rule.
public static void GenericSwap<T>(ref T reference1, ref T reference2)
{
T temp = reference1;
reference1 = reference2;
reference2 = temp;
}
}
class Test
{
static void Main()
{
string string1 = "Swap";
string string2 = "It";
object object1 = (object)string1;
object object2 = (object)string2;
ReferenceParameters.Swap(ref object1, ref object2);
string1 = (string)object1;
string2 = (string)object2;
Console.WriteLine("{0} {1}", string1, string2);
ReferenceParameters.GenericSwap(ref string1, ref string2);
Console.WriteLine("{0} {1}", string1, string2);
}
}
}
Imports System
Namespace DesignLibrary
Public NotInheritable Class ReferenceParameters
Private Sub New()
End Sub
' This method violates the rule.
Public Shared Sub Swap( _
ByRef object1 As Object, ByRef object2 As Object)
Dim temp As Object = object1
object1 = object2
object2 = temp
End Sub
' This method satifies the rule.
Public Shared Sub GenericSwap(Of T)( _
ByRef reference1 As T, ByRef reference2 As T)
Dim temp As T = reference1
reference1 = reference2
reference2 = temp
End Sub
End Class
Class Test
Shared Sub Main()
Dim string1 As String = "Swap"
Dim string2 As String = "It"
Dim object1 As Object = DirectCast(string1, Object)
Dim object2 As Object = DirectCast(string2, Object)
ReferenceParameters.Swap(object1, object2)
string1 = DirectCast(object1, String)
string2 = DirectCast(object2, String)
Console.WriteLine("{0} {1}", string1, string2)
ReferenceParameters.GenericSwap(string1, string2)
Console.WriteLine("{0} {1}", string1, string2)
End Sub
End Class
End Namespace
Related Rules
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
CA1003: Use generic event handler instances