CSharp examples for Custom Type:Generics
Here is a generic method that swaps the contents of two variables of any type T:
static void Swap<T> (ref T a, ref T b) { T temp = a; a = b; b = temp; }
Swap<T> can be used as follows:
int x = 5; int y = 10; Swap (ref x, ref y); static void Swap<T> (ref T a, ref T b) { T temp = a; a = b; b = temp; }
If there is ambiguity, generic methods can be called with the type arguments as follows:
Swap<int> (ref x, ref y);
Methods and types are the only constructs that can introduce type parameters.