Design Warnings


Visual Studio Team System
Generic methods should provide type parameter

TypeName

GenericMethodsShouldProvideTypeParameter

CheckId

CA1004

Category

Microsoft.Design

Breaking Change

Breaking

Cause

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

Rule Description

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.

How to Fix Violations

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.

When to Suppress Warnings

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.

Example

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.

Visual Basic
Imports System

Namespace DesignLibrary

   Public Class Inference

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

         Console.WriteLine(GetType(T))

      End Sub

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

         Console.WriteLine(sameAsTypeParameter)

      End Sub

   End Class

   Class Test

      Shared Sub Main()

         Dim infer As New Inference()
         infer.NotInferredTypeArgument(Of Integer)()
         infer.InferredTypeArgument(3)

      End Sub

   End Class

End Namespace
C#
using System;

namespace DesignLibrary
{
   public class Inference
   {
      // This method violates the rule.
      public void NotInferredTypeArgument<T>()
      {
         Console.WriteLine(typeof(T));
      }

      // This method satisfies the rule.
      public void InferredTypeArgument<T>(T sameAsTypeParameter)
      {
         Console.WriteLine(sameAsTypeParameter);
      }
   }

   class Test
   {
      static void Main()
      {
         Inference infer = new Inference();
         infer.NotInferredTypeArgument<int>();
         infer.InferredTypeArgument(3);
      }
   }
}
Related Rules

Avoid excessive parameters on generic types

Collections should implement generic interface

Do not declare static members on generic types

Do not expose generic lists

Do not nest generic types in member signatures

Use generic event handler instances

Use generics where appropriate

See Also

Reference

Tags :


Community Content

Ben Priebe
How can I modify this method to pass the rule?
public static void TestExceptionThrown<T>(Action method) where T : Exception
{
try
{
method();
Assert.Fail();
}
catch (T)
{
return;
}
Assert.Fail();
}
Tags :

Polymorpher
How can I modify this method to pass the rule?
Here is another example...I dont want to include a parameter for this, I just want to return my RAF of the given strut type...how can this be made to follow the rule?

Public Shared Function GetRandomAccessFile(Of T As Structure)(ByVal fileName As String) As IRandomAccessFile(Of T)

Return New RandomAccessFile(Of T)(fileName)

End Function

Tags :

Thomas Lee
RE: How can I modify this method to pass the rule?
Is anyone monitoring these threads? If your going to give us a set of guidlines, then the least you could do is give us some feedback when we try to stick to your guidlines.

[tfl - 03 10 09] Hi - and thanks for your post. You should post questions like this to the MSDN Forums at http://forums.microsoft.com/msdn or the MSDN Newsgroups at http://www.microsoft.com/communities/newsgroups/en-us/. You are much more likely get a quicker response using the forums than through the Community Content. For specific help about:
Visual Studio :
http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.vstudio%2C&
SQL Server :
http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.sqlserver%2C&
.NET Framework :
http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.dotnet.framework
PowerShell : http://groups.google.com/group/microsoft.public.windows.powershell/topics?pli=1
All Public : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public%2C&

Page view tracker