5.4. Type Parameter Names

The introduction of generic methods can create a problem in terms of naming your type parameters. As a rule of thumb, you should typically use a common naming scheme for your type parameters. For example, a key/value pair of type parameters would typically be assigned TKey and TValue as parameter names. As discussed earlier, this approach brings consistency to your naming and, in turn, improves the general readability and maintainability of your classes.

This general approach can complicate the naming of your type parameters for generic methods. Consider a situation in which you have a generic class that accepts key/value type parameters and, within that class, you also have a method that accepts its own key/value pair of type parameters. If you're trying to use consistent naming, you'd want to use the TKey and TValue parameter names for your class and your method. However, following this naming scheme also causes your class and method parameter names to collide. The type for TKey and TValue essentially become ambiguous. In fact, as you saw in the previous example, using the same type parameter names for a method and class ends up hiding your class's type parameters from your method.

For these scenarios, you may want to consider adopting an alternative naming scheme for your generic method's type parameters that can achieve uniformity while minimizing its potential to collide with the type parameter names being used by your generic classes. Naturally, ...

Get Professional .NET 2.0 Generics now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.