Generic methods allow developers to write methods that work with different data types using type parameters. They provide code reusability and compile-time type safety without requiring separate implementations for each data type.
- Generic methods can be defined in both generic and non-generic classes.
- They use type parameters such as T to represent different data types.
- The compiler can often infer type arguments automatically from method arguments.
Example: Program to demonstrate a simple generic method that displays values of different data types.
using System;
class Geeks{
static void Display<T>(T value){
Console.WriteLine("Value: " + value);
}
static void Main()
{
Display<int>(100);
Display<string>("GeeksForGeeks");
// Type inferred as double
Display(99.99);
}
}
Output
Value: 100 Value: GeeksForGeeks Value: 99.99
Explanation:
- Display<int>(100) uses int as the type argument.
- Display<string>("GeeksForGeeks") uses string.
- Display(99.99) allows the compiler to infer double automatically.
Syntax
ReturnType MethodName<T>(T parameter){
// Method body
}
Here:
- T represents a type parameter.
- MethodName<T> declares a generic method.
- The actual type can be specified explicitly or inferred by the compiler when the method is called.

Example: Program to demonstrate a generic method that swaps values of different data types without requiring separate implementations.
using System;
class Geeks {
static void Swap<T>(ref T a, ref T b){
T temp = a;
a = b;
b = temp;
}
static void Main(){
int x = 10, y = 20;
Swap(ref x, ref y);
Console.WriteLine($"x = {x}, y = {y}");
string s1 = "Hello", s2 = "World";
Swap(ref s1, ref s2);
Console.WriteLine($"s1 = {s1}, s2 = {s2}");
}
}
Output
x = 20, y = 10 s1 = World, s2 = Hello
Explanation: The Swap<T>() method works with both integers and strings. The ref keyword allows the method to modify the original variables, while the compiler infers the type parameter from the supplied arguments.
Generic Method with Multiple Type Parameters
A generic method can declare multiple type parameters to accept values of different types.
using System;
class Geeks {
static void ShowData<T1, T2>(T1 first, T2 second){
Console.WriteLine($"First: {first}, Second: {second}");
}
static void Main()
{
ShowData<int, string>(1, "One");
ShowData<string, double>("Pi", 3.14);
}
}
Output
First: 1, Second: One First: Pi, Second: 3.14
Explanation: T1 and T2 represent independent type parameters, allowing the method to accept values of different types in a single call.
Generic Method Constraints
Generic constraints restrict which types can be used as type arguments. They are defined using the where keyword.
Example: Program to demonstrate a generic method that accepts only non-nullable value types using the struct constraint.
using System;
class Geeks {
static void DisplayValue<T>(T value) where T : struct{
Console.WriteLine(value);
}
static void Main()
{
DisplayValue(100);
DisplayValue(99.5);
}
}
Output
100 99.5
The where T : struct constraint ensures that T is a non-nullable value type, such as int, double, or a user-defined structure.
Advantages of Generic Methods
- Type Safety: Detects incompatible type usage at compile time.
- Code Reusability: Allows one method to work with multiple data types.
- Performance: Avoids boxing and unboxing that may occur when value types are handled through object.
- Flexibility: Supports multiple type parameters and constraints for different requirements.

