Skip to content

Generic Methods

Context: A generic method declares its own type parameters, independent of the class (which may also be generic or not). This allows you to create a method that works with any type, with compile-time type safety.

using System;
public class Utility
{
// Generic method
public T Max<T>(T a, T b) where T : IComparable<T>
{
return a.CompareTo(b) > 0 ? a : b;
}
}
class Program
{
static void Main()
{
Utility util = new Utility();
int maxInt = util.Max(5, 10);
Console.WriteLine(maxInt);
string maxString = util.Max("apple", "banana");
Console.WriteLine(maxString);
}
}
Terminal window
dotnet run
10
apple
  • The type parameter is declared after the method name and before the parameter list.
  • Generic methods can be overloaded.
  • Constraints apply to the method’s type parameters.

LINQ – Methods like Select, Where, OrderBy are generic methods that work with IEnumerable<T>.
See .NET docs on generic methods.