Using Generic Methods

So much attention gets paid to the new generic classes that have been introduced with .NET generics, I often find developers overlooking what can be achieved through the use of generic methods. In fact, in many cases, you may discover one of the first opportunities to leverage generics is by introducing some generic methods into your non-generic types. This brings the flavor and power of generics to a class without requiring the class itself to be parameterized.

The Basics

To illustrate the fundamental value of generic methods, let's start with the simplest of examples. Suppose you have a Max() function that accepts two double values, compares them, and returns the greater of the two values. This function might appear as follows:

[VB code]
Public Function Max(ByVal val1 As Double, ByVal val2 As Double) 
As Double
    Return IIf(val2 < val1, val1, val2)
End Function
[C# code]
public double Max(double val1, double val2) {
    return (val2 < val1) ? val1 : val2;
}

This method is handy for number-crunching applications. However, once you decide you want to apply this same Max() function to additional data types, you have a problem. This method can only be applied to double data types. You only have a few real, type-safe options that you can use to resolve this problem. One approach would be to create specific versions of this method to support each data type. However, doing that would force you to bloat your namespace with MaxString, MaxInt, and MaxLong methods. Not good. To get around the bloat issue, you might consider going back to using an object-based interface and tossing all type safety to the wind. Your last option here would be to provide several overloaded versions of Max() that accepted different types. That might represent some measure of improvement, but it's still not ideal.

This discussion of taking on bloat or compromising type safety is probably starting to sound like a broken record at this point. You see the same patterns over and over again in your code. You start out with a nice, general-purpose class or method only to find that, as you attempt to broaden its applicability, you discover that the tools offer you few good options to extrapolate that generality to additional data types. That's right in the sweet spot of generics.

So, let's look at how generics can be applied to the Max() method. The following code represents the generic version of the Max() method:

[VB code]
Public Function Max(Of T As IComparable)
(ByVal val1 As T, ByVal val2 As T) As T
    Dim retVal As T = val2
    If (val2.CompareTo(val1) < 0) Then
        retVal = val1
    End If
    Return retVal
End Function
[C# code]
public T Max<T>(T val1, T val2) where T : IComparable  {
    T retVal = val2;
    if (val2.CompareTo(val1) < 0)
        retVal = val1;
    return retVal;
}

The syntax and concepts here are right in line with what you've already seen with generic classes. The Max() method becomes a parameterized type, accepting one or more type parameters as part of its signature. Once you've outfitted your method with a type parameter, you can then proceed to reference that type parameter throughout the scope of the function. Method parameters, return types, and types appearing in the body of your methods may all reference the type parameters that are supplied to your generic method.

For this example to work properly, I was required to apply a constraint to my type parameter, indicating that each T must implement IComparable. Constraints are addressed in detail in the book Professional .NET 2.0 Generics Chapter 7, "Generic Constraints."

All that remains at this stage is to start making some calls to this new, generic Max() method. Let's take a quick look at how clients would invoke the Max() method with a few different type arguments:

[VB code]
Dim doubleMax As Double = Max(Of Double)(3939.99, 39999.99)
Dim intMax As Int32 = Max(Of Double)(339, 23)
Dim stringMax As String = Max(Of String)("AAAA", "BBBBBB")
[C# code]
double doubleMax = Max<double>(3939.99, 39999.99);
int intMax = Max<int>(339, 23);
string stringMax = Max<string>("AAAA", "BBBBBB");

Calling a generic method, as you can see, is not all that different than calling a non-generic method. The only new wrinkle here is the introduction of a type parameter immediately following the name of the method.

Using Generic Methods

A Deeper Look

With the conceptual introduction behind us, let's now consider some more detailed examples of generic methods. The following example defines a class, SimpleClass, which declares a series of generic methods that illustrate some of the variations that are available to you when creating your own generic methods. To make things more interesting, all of these generic methods in this example are placed within a generic class. The code for the class is as follows:

[VB code]
Public Class SimpleClass(Of T, U)
    Private _outerVal1 As T
    Private _outerVal2 As U
        Public Sub New(ByVal val1 As T, ByVal val2 As U)
        Me._outerVal1 = val1
        Me._outerVal2 = val2
    End Sub
        Public Sub Foo1(Of I)(ByVal innerVal As I)
        Console.Out.WriteLine("Method Param Type  : {0}", 
            innerVal.GetType())
        Console.Out.WriteLine("Class Param Type(T): {0}", 
            _outerVal1.GetType())
    End Sub
        Public Function Foo2(Of I)(ByVal innerVal As I) As U
        Console.Out.WriteLine("Method Param Type    : {0}",
            innerVal.GetType())
        Console.Out.WriteLine("Method Return Type(U): {0}",
            _outerVal1.GetType())
        Return _outerVal2
    End Function
        Public Sub Foo3(Of T)(ByVal innerVal As T)
        Console.Out.WriteLine("Method Param Type  : {0}", 
            innerVal.GetType())
    End Sub
        Public Shared Function Foo4(Of I, J)
        (ByVal val1 As I, ByVal val2 As J, _
        ByVal outer As U) As Nullable(Of T)
Dim retVal As New Nullable(Of T)
Console.Out.WriteLine("Static Method Param1 Type : {0}",
    val1.GetType())
Console.Out.WriteLine("Static Method Param2 Type : {0}",
    val2.GetType())
Console.Out.WriteLine("Static Method Param3 Type : {0}",
    outer.GetType())
Console.Out.WriteLine("Static Method Return Type : {0}",
    retVal.GetType())
Return retVal
    End Function
End Class
[C# code]
public class SimpleClass<T, U> {
    private T _outerVal1;
    private U _outerVal2;    public SimpleClass(T val1, U val2) {
        this._outerVal1 = val1;
        this._outerVal2 = val2;
    }
        public void Foo1<I>(I innerVal) {
Console.Out.WriteLine("Method Param Type  : {0}",
        innerVal.GetType());
Console.Out.WriteLine("Class Param Type(T): {0}", _outerVal1.GetType());
    }    public U Foo2<I>(I innerVal) {
Console.Out.WriteLine("Method Param Type    : {0}", 
        innerVal.GetType());
Console.Out.WriteLine("Method Return Type(U): {0}", _outerVal1.GetType());
        return _outerVal2;
    }
        public void Foo3<T>(T innerVal) {
        Console.Out.WriteLine("Method Param Type  : {0}",
       innerVal.GetType());
    }
        public static Nullable<T> Foo4<I, J>(I val1, J val2, U outer) {
        Nullable<T> retVal = default(T);
        Console.Out.WriteLine("Static Method Param1 Type : {0}",
            val1.GetType());
        Console.Out.WriteLine("Static Method Param2 Type : {0}",
            val2.GetType());
        Console.Out.WriteLine("Static Method Param3 Type : {0}",
            outer.GetType());
        Console.Out.WriteLine("Static Method Return Type : {0}",
            retVal.GetType());
return retVal;
    }
}

This generic class accepts two type parameters, T and U, and implements four different generic methods. Let's start by looking at the Foo1() method, which accepts a single type parameter I. What's slightly different here is that this method also accesses the T type parameter that belongs to the surrounding class. The goal here is simply to illustrate the accessibility of the surrounding class's type parameters. Every generic method declared in this class may, within any part of its implementation, reference the type parameters that are associated with that class. So, this particular example includes a line of code that writes out the type of the class's T type parameter.

From the perspective of a generic method, type parameters should be treated like any other type that would traditionally be declared within the scope of your class. The rules that govern your method's ability to reference type parameters conform to the same rules that govern the use of non-generic type parameters. The Foo2() method demonstrates one more variation on this theme, referencing the type parameter U as its return type. The idea here is that you shouldn't limit your view of type parameters to just those supplied in the declaration of your method. Leveraging the type parameters of your class and your method in tandem creates a broader view of what can be achieved with a generic method.

The Foo3() method exposes another issue you need to consider when using generic methods within a generic class. The type parameter it accepts shares the same name, T, as a type parameter used by the surrounding class. At a minimum, this creates some confusion. It's not clear what type T will ultimately be assigned. If you run this method in the debugger, however, you'll quickly discover that the T type parameter for Foo3() will always be the type supplied in the call to the method. This, in effect, ends up preventing this method from making any use of the T type parameter that is part of SimpleClass. Clearly, this practice limits the versatility of your generic methods and should be discouraged. Even if your class doesn't need to leverage the type parameters from the class, it would still create some degree of confusion to have these type parameters share the same name. Fortunately, the compiler will generate a warning in this scenario. So, if you happen to do this unintentionally, you'll be notified.

Generic methods may also be static, as demonstrated by the Foo4() method of this example. This method actually illustrates a few separate points. First, it's static and, as such, can be invoked without requiring clients to create an instance of SimpleClass. Even though it's static, it can still reference the type parameters from the class. At first glance, that may seem wrong. However, remember that the type parameters do not reference instance data, they simply define types. This means they can be littered freely throughout your static methods. For this method, U is used as the type of the third parameter and T as part of the return type. Finally, for one last bit of variety, this method also illustrates the use of multiple type parameters.

Calling this static method follows the same conventions as you've seen with calling static methods on generic classes. The only exception is the addition of the new type arguments that accompany the method. Let's look at a small snippet of code that calls the static Foo4() method to see what this might look like:

[VB code]
SimpleClass(Of DateTime, String).Foo4(Of Int32, Double)
        (42, 323.3234, "A Param")
[C# code]
SimpleClass<DateTime, String>.Foo4<int, double>
        (42, 323.3234, "A Param");

You'll notice that this combination of a static generic method within a generic class, both of which accept two type parameters, gets a little unwieldy. Still, it can't be accused of lacking expressiveness or type safety.

Applying Constraints

Anywhere you're allowed to create a generic type, you are also allowed to qualify that type with constraints. For a generic method, the application of constraints to the incoming type parameters is relatively simple. Chapter 7 "Generic Constraints" in the book Professional .NET 2.0 Generics looks at constraints in detail, but here's a quick peek at the mechanics of how constraints are applied to the type parameters of your generic methods:

[VB code]
Public Sub Foo(Of I As IComparable)(ByVal val1 As I)
End Sub
[C# code]
public void Foo<I>(I val1) where I : IComparable {}

As you can see, the syntax here leverages the same approach that you may have seen applied to generic classes. You simply add the interface qualifier for a given type parameter and that will allow you to treat any reference to that the type parameter as conforming to the supplied interface.

Using Generic Methods

Overriding Generic Methods

Generic methods may also be overridden by descendant classes. In fact, generic methods introduce surprisingly few new facets when it comes when overriding methods. The syntax for overriding generic methods follows the same pattern as non-generic methods where the overriding method must match, precisely, the parameters of the parent method. With a generic method, the only difference is that the parameters can be expressed as type parameters. Here's a simple example:

[VB code]
Public Class Person(Of T, U)
Public Overridable Sub Foo1(Of I)(ByVal val1 As T)
    End Sub
Public Overridable Sub Foo1(Of I)(ByVal val1 As Int32)
    End Sub
Public Overridable Sub Foo2(Of I, J)(ByVal val1 As U)
    End Sub
Public Overridable Sub Foo3(Of I, U)(ByVal val1 As I, ByVal val2 As U)
    End Sub
Public Overridable Sub Foo4(Of D, E, F)(ByVal val1 As D, ByVal val2 As E)
    End Sub
Public Overridable Function Foo5(Of I As IComparable)
    (ByVal val1 As I) As I
    End FunctionEnd ClassPublic Class Employee(Of T, U)
    Inherits Person(Of String, U)
'Error: can't verify this is unique for all permutations
Public Overrides Sub Foo1(Of I)(ByVal val1 As Int32)
    End Sub
Public Overrides Sub Foo2(Of I, J)(ByVal val1 As U)
    End Sub
Public Overrides Sub Foo3(Of I, U)(ByVal val1 As I, ByVal val2 As U)
    End Sub
Public Overrides Sub Foo4(Of A, B, C)(ByVal val1 As A, ByVal val2 As B)
    End Sub
End Class
[C# code]
public class Person<T, U> {
    public virtual void Foo1<I>(T val1) {}
    public virtual void Foo1<I>(int val1) {}
    public virtual void Foo2<I, J>(U val1) {}
    public virtual void Foo3<I, U>(I val1, U val2) {}
    public virtual void Foo4<D, E, F>(D val1, E val2) {}
    public virtual I Foo5<I>(I val1) where I : IComparable {
        return default(I);
    }
}public class Employee<T, U> : Person<string, U> {
    public override void Foo1<I>(int val1) {}
    public override void Foo2<I, J>(U val1) {}
    public override void Foo3<I, U>(I val1, U val2) {}
    public override void Foo4<A, B, C>(A val1, B val2) {}
}

A series of examples are shown in this section, each of which attempts to override a generic method. The goal here is to provide a sampling of permutations so you can have a better feel for what's possible. This example sets things up by declaring a generic class, Person, and creating a descendant generic Employee class that overrides a handful of its parent's virtual, generic methods.

Most of the overrides, at this stage, are just as you would expect. The overriding method simply matches the signature of its parent. You should pay particular attention the role type parameters play in this example. In some instances, the type parameters of the surrounding class are referenced and, in others, the generic methods reference their own type parameters. The Foo2() method, for example, accepts type parameters of I and J and references the U type parameter that is part of the class declaration.

The other method here that offers a slight twist is Foo4(). This method matches the parent's signature but uses entirely different type parameter names. This is only meant to demonstrate that — even in an overriding scenario — the names of the type parameters are still just placeholders. The fact that these names are different in the base class does not prevent you from successfully overriding it with alternate type parameter names.

This first example (and those that follow) demonstrates a few areas where VB and C# diverge in their approach to overriding generic methods. In this first set of examples, C# compiles both of these classes successfully. However, VB throws an error on the Foo1() here. It preemptively determines that there are instances where the type for the T parameter can make overloaded versions of Foo1() that collide.

The next example takes this a little further and adds another class that changes the inheritance scheme. The following generic Customer class also extends the Person class and overrides two of its generic methods:

[VB code]
Public Class Customer(Of T, U)
    Inherits Person(Of T, U)
        'Error: can't verify this is unique for all permutations
    Public Overrides Sub Foo1(Of I)(ByVal val1 As T)
    End Sub
        Public Overrides Function Foo5(Of I As IComparable)
            (ByVal val1 As I) As I
    End Function
End Class
[C# code]
public class Customer<T, U> : Person<T, U> {
    public override void Foo1<I>(T val1) {}
    public override I Foo5<I>(I val1) {
        return default(I);
    }
}

In contrast with the previous example, this class uses the T and U type parameters in its inheritance declaration. By referencing the same type parameter for T in both the base and descendant class, you are able to override the Foo1() method that references the T parameter in the base class. This is only possible because the T in both classes is guaranteed to reference the same type. Of course, Foo1() fails in the VB example again for the same reasons discovered in the previous example.

The other override here, the Foo5() method, demonstrates how constraints factor into the signature of a generic method that's being overridden. Here, you might think that Foo5() would not successfully override the declaration in its parent, because the Person class included a constraint as part of its declaration. For C#, the inclusion of the matching constraint would actually generate a compile-time error here. When constraints are part of the base class in C#, the overriding method always inherits the constraints and cannot alter them. The opposite is true in VB, where the overriding method is required to include the constraint as part of the method's signature. The rationale behind this inconsistency is not clear.

There's one final scenario worth exploring. You'll notice that the Person class actually includes an overloaded method, Foo1(). This method has one version that accepts a T type parameter and the other accepts an integer. Now, consider this example where the T type argument supplied to the parent is an integer:

[VB code]
Public Class Vendor(Of T, U)
    Inherits Person(Of Int32, U)
        Public Overrides Sub Foo1(Of I)(ByVal val1 As Int32)
    End Sub
End Class
[C# code]
public class Vendor<T, U> : Person<int, U> {
    public override void Foo1<I>(int val1) {}
}

This class would seem to be valid. Its declaration of the Foo1() method certainly matches that of the parent class. The problem here isn't that the method doesn't match — it's that two methods from the Person class both match this signature. This issue is caused by the use of an integer in its inheritance from the Person class. That integer causes that the Foo1(T val1) method to collide with the other Foo1() declaration.

As noted earlier, this is one area where VB and C# vary in their handling of the Foo1() method. VB identifies this error at the point of declaration, whereas C# won't throw the error until a type argument is supplied that creates a situation where the signatures of the overloaded methods collide.

Using Generic Methods

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 SubInferParams(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 supplied 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 overloaded 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 example 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.

As you can see, generic methods are quite handy. They bring a whole new set of possibilities to how you approach your methods — especially those methods that may currently struggle with type-safety issues. And, thanks to type inference, consumers are often able to leverage generic methods without any direct awareness of any generic syntax. Authors of generic methods, on the other hand, do have some new factors to take into consideration when composing generic methods. Still, the handful of new rules that accompany generic methods are largely intuitive and most developers should be able to make the transition to generic methods with relative ease.

This article is adapted from Professional .NET 2.0 Generics by (Wrox, 2005, ISBN: 0-7645-5988-5), from chapter 5 "Generic Methods." Tod Golding's other recent article on CodeGuru.com is a related article "Understanding Generic Classes" adapted from chapter 4 "Generic Clasess" in Professional .NET 2.0 Generics.



About the Author

Tod Golding

Tod Golding has 20 years of experience as a software developer, lead architect, and development manager for organizations engaged in the delivery of large-scale commercial and internal solutions. He has an extensive background leveraging .NET, J2EE, and Windows DNA technologies, and is skilled with C#, Java, and C++. Tod is the author of Professional .NET 2.0 Generics published by WROX.

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • The hard facts on SaaS adoption in over 80,000 enterprises: Public vs. private companies Mid-market vs. large enterprise GoogleApps, Office365, Salesforce & more Why security is a growing concern Fill out the form to download the full cloud adoption report.

  • On-demand Event Event Date: October 29, 2014 It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this webcast, Gene Kim discusses these survey findings and shares woeful tales of artifact management gone wrong! Gene also shares examples of how high-performing DevOps …

Most Popular Programming Stories

More for Developers

RSS Feeds