Multiple constraints
Context: Multiple constraints can be applied to a single type parameter. The order must be: base class (or class/struct), interfaces, new(). Multiple constraints increase the capabilities of the generic type.
Usage Example
Section titled “Usage Example”using System;using System.Collections.Generic;
public interface IComparableWithDisplay<T> : IComparable<T>{ void Display();}
public class Person : IComparableWithDisplay<Person>{ public string Name { get; set; } public int Age { get; set; }
public int CompareTo(Person other) { return Age.CompareTo(other.Age); }
public void Display() => Console.WriteLine($"{Name} ({Age} years)");}
public class Utility<T> where T : class, IComparableWithDisplay<T>, new(){ public T CreateAndDisplay() { T obj = new T(); obj.Display(); return obj; }
public T Max(T a, T b) { return a.CompareTo(b) > 0 ? a : b; }}
class Program{ static void Main() { var util = new Utility<Person>(); var p1 = new Person { Name = "Alice", Age = 30 }; var p2 = new Person { Name = "Bob", Age = 25 };
Person older = util.Max(p1, p2); Console.WriteLine($"Older: {older.Name}"); }}Output console
Section titled “Output console”dotnet runOlder: AliceImportant notes
Section titled “Important notes”- Order: base class (or
class/struct) first, then interfaces, thennew(). - You cannot combine
classandstruct. - Constraints increase readability but also rigidity.
Real-world usage example
Section titled “Real-world usage example”Dictionary<TKey, TValue> – TKey must implement IEquatable<TKey> or object.Equals (implicit constraint).
See .NET docs on multiple constraints.