Generics
Generic Constraints
A generic constraint requires a type parameter to provide certain members.
constraint
A constraint limits which types can be used with a generic method or class.
Generic Constraints
GenericConstraints.cs
Replay: real traced execution (multi-file project)
using System;
interface INamed
{
string Name { get; }
}
class Customer : INamed
{
public string Name { get; }
public Customer(string name)
{
Name = name;
}
}
class Program
{
static string Label<T>(T item) where T : INamed
{
return "name:" + item.Name;
}
static void Main()
{
string name = "Mira";
Customer customer = new Customer(name);
string label = Label(customer);
Console.WriteLine($"name={name}");
Console.WriteLine($"label={label}");
}
}
using System;
interface INamed
{
string Name { get; }
}
class Customer : INamed
{
public string Name { get; }
public Customer(string name)
{
Name = name;
}
}
class Program
{
static string Label<T>(T item) where T : INamed
{
return "name:" + item.Name;
}
static void Main()
{
string name = "Noah";
Customer customer = new Customer(name);
string label = Label(customer);
Console.WriteLine($"name={name}");
Console.WriteLine($"label={label}");
}
}
using System;
interface INamed
{
string Name { get; }
}
class Customer : INamed
{
public string Name { get; }
public Customer(string name)
{
Name = name;
}
}
class Program
{
static string Label<T>(T item) where T : INamed
{
return "name:" + item.Name;
}
static void Main()
{
string name = "Zoe";
Customer customer = new Customer(name);
string label = Label(customer);
Console.WriteLine($"name={name}");
Console.WriteLine($"label={label}");
}
}
name ← Mira
25static void Main()26{27 string name→ Mira = "Mira"; //@name="Noah", "Zoe"28 Customer customer = new Customer(name);29 string label = Label(customer);public Customer(string name)
12public Customer(string nameMira)13{14 Name = nameMira;15}customer ← Customer
27string name = "Mira"; //@name="Noah", "Zoe"28Customer customer→ Customer = new Customer(name);29string label = Label(customerCustomer);static string Label<T>(T item) where T : INamed
19{20 static string Label<T>(T itemCustomer) where T : INamed21 {22 return "name:" + item.NameMira;23 }label ← name:Mira
28 Customer customer = new Customer(name);29 string label→ name:Mira = Label(customerCustomer);3031 Console.WriteLine($"name={nameMira}");32 Console.WriteLine($"label={labelname:Mira}");33}outputname=Mira label=name:Mira
name ← Noah
25static void Main()26{27 string name→ Noah = "Noah";28 Customer customer = new Customer(name);29 string label = Label(customer);public Customer(string name)
12public Customer(string nameNoah)13{14 Name = nameNoah;15}customer ← Customer
27string name = "Noah";28Customer customer→ Customer = new Customer(name);29string label = Label(customerCustomer);static string Label<T>(T item) where T : INamed
19{20 static string Label<T>(T itemCustomer) where T : INamed21 {22 return "name:" + item.NameNoah;23 }label ← name:Noah
28 Customer customer = new Customer(name);29 string label→ name:Noah = Label(customerCustomer);3031 Console.WriteLine($"name={nameNoah}");32 Console.WriteLine($"label={labelname:Noah}");33}outputname=Noah label=name:Noah
name ← Zoe
25static void Main()26{27 string name→ Zoe = "Zoe";28 Customer customer = new Customer(name);29 string label = Label(customer);public Customer(string name)
12public Customer(string nameZoe)13{14 Name = nameZoe;15}customer ← Customer
27string name = "Zoe";28Customer customer→ Customer = new Customer(name);29string label = Label(customerCustomer);static string Label<T>(T item) where T : INamed
19{20 static string Label<T>(T itemCustomer) where T : INamed21 {22 return "name:" + item.NameZoe;23 }label ← name:Zoe
28 Customer customer = new Customer(name);29 string label→ name:Zoe = Label(customerCustomer);3031 Console.WriteLine($"name={nameZoe}");32 Console.WriteLine($"label={labelname:Zoe}");33}outputname=Zoe label=name:Zoe