|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.|
Generic Methods (C# Programming Guide)
A generic method is a method that is declared with type parameters, as follows:
The following code example shows one way to call the method by using int for the type argument:
You can also omit the type argument and the compiler will infer it. The following call to Swap is equivalent to the previous call:
The same rules for type inference apply to static methods and instance methods. The compiler can infer the type parameters based on the method arguments you pass in; it cannot infer the type parameters only from a constraint or return value. Therefore type inference does not work with methods that have no parameters. Type inference occurs at compile time before the compiler tries to resolve overloaded method signatures. The compiler applies type inference logic to all generic methods that share the same name. In the overload resolution step, the compiler includes only those generic methods on which type inference succeeded.
Within a generic class, non-generic methods can access the class-level type parameters, as follows:
If you define a generic method that takes the same type parameters as the containing class, the compiler generates warning CS0693 because within the method scope, the argument supplied for the inner T hides the argument supplied for the outer T. If you require the flexibility of calling a generic class method with type arguments other than the ones provided when the class was instantiated, consider providing another identifier for the type parameter of the method, as shown in GenericList2<T> in the following example.
Use constraints to enable more specialized operations on type parameters in methods. This version of Swap<T>, now named SwapIfGreater<T>, can only be used with type arguments that implement IComparable<T>.
Generic methods can be overloaded on several type parameters. For example, the following methods can all be located in the same class: