Generic methods should provide type parameter
Collapse the table of content
Expand the table of content

Generic methods should provide type parameter







Breaking Change


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 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 using inference for all type parameters, the syntax for calling generic and non-generic 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 an identical type for each of the type parameters of the method.

Do not exclude 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, while the type argument for NotInferredTypeArgument must be explicitly specified.

Imports System

Namespace DesignLibrary

   Public Class Inference

      ' This method violates the rule.
      Sub NotInferredTypeArgument(Of T)()


      End Sub
      ' This method satisfies the rule.
      Sub InferredTypeArgument(Of T)(sameAsTypeParameter As T)


      End Sub

   End Class

   Class Test
      Shared Sub Main()
         Dim infer As New Inference()
         infer.NotInferredTypeArgument(Of Integer)()

      End Sub

   End Class

End Namespace

Community Additions

© 2015 Microsoft