We recommend using Visual Studio 2017

CA1004: Generic methods should provide type parameter


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 CA1004: Generic methods should provide type parameter.

|Breaking Change|Breaking|

The parameter signature of an externally visible generic method does not contain types that correspond to all the type parameters of the method.

Inference is how the type argument of a generic method is determined by the type of argument that is passed to the method, instead of by the explicit specification of the type argument. To enable inference, the parameter signature of a generic method must include a parameter that is of the same type as the type parameter for the method. In this case, the type argument does not have to be specified. When you use inference for all type parameters, the syntax for calling generic and nongeneric instance methods is identical. This simplifies the usability of generic methods.

To fix a violation of this rule, change the design so that the parameter signature contains the same type for each type parameter of the method.

Do not suppress a warning from this rule. Providing generics in a syntax that is easy to understand and use reduces the time that is required to learn and increases the adoption rate of new libraries.

The following example shows the syntax for calling two generic methods. The type argument for InferredTypeArgument is inferred, and the type argument for NotInferredTypeArgument must be explicitly specified.

using System;

namespace DesignLibrary
   public class Inference
      // This method violates the rule.
      public void NotInferredTypeArgument<T>()

      // This method satisfies the rule.
      public void InferredTypeArgument<T>(T sameAsTypeParameter)

   class Test
      static void Main()
         Inference infer = new Inference();

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

CA1003: Use generic event handler instances

CA1007: Use generics where appropriate