C# Generic Methods
In my previous post, I explained about Generics. C# also provides Generic Methods. We can create a method which defer the parameter data type until the method is called. These parameters are called Type parameters that means we can pass the actual data type later.
Below is the example of Generic Method.
static void Swap<T>(ref T input1, ref T input2)
{
T temp = default(T);
temp = input2;
input2 = input1;
input1 = temp;
}
static void Main(string[] args)
{
int first = 4;
int second = 5;
Swap<int>(ref first, ref second);
}
In the above example, we have declared a Swap generic method. In that method we have declared a Type argument after the method name <T>. T is the type argument. In the main method, we pass Swap method with <int> type argument. That means, we can only pass parameters of int types. We declared two int parameters and pass into the Swap method.
Generic Methods and Generic Classes
Generic method type parameter hides the type parameter of Generic classes. CLR will not issue any warnings or errors, when we use the same type parameter name with both generic method and generic class.
Below is the example:
public class Helper<T>
{
public void Method<T>(T input)
{
Console.WriteLine(input); //Result Hello
}
}
class Program
{
static void Main(string[] args)
{
Helper<int> helper = new Helper<int>();
helper.Method<string>("Hello");
//Result:
// Hello
}
}
In the above generic method example, we have taken the same type parameter name which is used by generic class. In the main method, we have passed the <int> type argument in the Helper class. In the method, we have passed the <string> argument.
Generic Overloaded Methods
C# allows you to define generic overloaded methods with many type parameters.
Below is the example:
static void Swap<T>(T input) { }
static void Swap<T, U>(T input, U input2) { }
static void Swap<T, U, W>(T input, U input2, W input3) { }
Generic Methods Constraints
Constraints are validations on type arguments which type of parameter we can pass in generic method. Generic class and Generic methods follow the same constraints.
There are six types of constraints.
- where T : struct – Type argument must be a value type
- where T : class – Type argument must be a reference type
- where T : new() – Type argument must have a public parameterless constructor.
- where T : <base class> – Type argument must inherit from <base class> class.
- where T : <interface> – Type argument must implement from <interface> interface.
- where T : U – There are two type arguments T and U. T must be inherit from U.
Below is the example of usage of above constraints in Generic methods.
static void Swap<T>(ref T input1, ref T input2) where T : struct { }
static void Swap<T>(ref T input1, ref T input2) where T : class { }
static void Swap<T>(ref T input1, ref T input2) where T : new() { }
static void Swap<T>(ref T input1, ref T input2) where T : BaseEmployee { }
static void Swap<T>(ref T input1, ref T input2) where T : IEmployee { }
static void Swap<T, U>(ref T input1, ref U input2) where T : U { }
Final Words
Generic classes and Generic Methods are great tools for C# programmers. With both, we can create generic classes and methods which can work with any data type.