Type Inference
One really handy aspect of generic methods is their ability to examine the types of incoming arguments
and infer which method should be invoked. This eliminates the need to explicitly specify type arguments
when calling a generic method. Consider the following method declaration and corresponding calls:
[VB code]
Public Sub InferParams(Of I, J)(ByVal val1 As I, ByVal val2 As J)
Console.Out.WriteLine("I: {0}, J: {1}", val1.GetType(), val2.GetType())
End Sub
InferParams(Of Int32, String)(14, "Test")
InferParams("Param1", 3939.39)
InferParams(93, "Param2")
[C# code]
public void InferParams<I, J>(I val1, J val2) {
Console.Out.WriteLine("I: {0}, J: {1}", val1.GetType(), val2.GetType());
}
InferParams<int, string>(14, "Test");
InferParams("Param1", 3939.39);
InferParams(93, "Param2");
This example declares a generic method that accepts two type parameters and provides three examples
of calls to that method. The first call provides explicit type arguments. In this case, the types of the sup-
plied parameter must match the types specified in the type parameter list. The next two examples both
successfully call the
InferParams()
method without supplying any type arguments. They both use
type inference, where the type is inferred from the types of the supplied arguments.
Leveraging this mechanism makes sense in most situations. However, in instances where you've over-
loaded a method, you may encounter some degree of ambiguity. Suppose you were to add the following
overloaded method to the preceding example:
[VB code]
Public Sub InferParams(Of I, J)(ByVal val1 As Int32, ByVal val2 As String)
Console.Out.WriteLine("I: {0}, J: {1}", val1.GetType(), val2.GetType())
End Sub
[C# code]
public void InferParams<I, J>(int val1, string val2) {
Console.Out.WriteLine("I: {0}, J: {1}", val1.GetType(), val2.GetType());
}
This method overloads the previous
InferParams()
method, adding a version that includes specific
integer
and
string
types in its parameter list. Now, when you execute the sample calls to this
method, it's not clear, from looking at the code, which method will get called. As it turns out, the exam-
ple that supplied explicit type parameters would get you into this new method, and the version that
infers the parameter types will get you into the other version of this method. Still, if you're overloading
like this, it's probably not wise to rely on inference for your type parameters.
95
Generic Methods
08_559885 ch05.qxd 9/8/05 11:00 PM Page 95