泛型约束小计:

1 class Program 2 { 3 static void Main(string[] args) 4 { 5 { 6 var generice = new GenericClass(); 7 8 generice.ShowInt(11); 9 generice.ShowDateTime(DateTime.Now); 10 generice.ShowPeople(new People {Id = 11, Name = "Tom"}); 11 } 12 13 { 14 Console.WriteLine("---------------------------------------"); 15 var generice = new GenericClass2(); 16 generice.ShowObj(11); 17 generice.ShowObj(DateTime.Now); 18 generice.ShowObj(new People {Id = 11, Name = "Tom"}); 19 } 20 21 { 22 Console.WriteLine("---------------------------------------"); 23 var generice = new GenericClass3(); 24 generice.ShowT<int>(11); 25 generice.ShowT<DateTime>(DateTime.Now); 26 generice.ShowT<People>(new People { Id = 11, Name = "Tom" }); 27 } 28 29 Console.ReadKey(); 30 } 31 } 32 public class GenericClass3 33 { 34 public void ShowT<T>(T t) 35 { 36 Console.WriteLine("ShowT print {0},ShowT Parament Type Is {1}", t, t.GetType()); 37 } 38 } 39 40 public class GenericClass2 41 { 42 public void ShowObj(object obj) 43 { 44 Console.WriteLine("ShowObj print {0},ShowObj Parament Type Is {1}", obj, obj.GetType()); 45 } 46 } 47 48 public class GenericClass 49 { 50 public void ShowInt(int n) 51 { 52 Console.WriteLine("ShowInt print {0},ShowInt Parament Type Is {1}", n, n.GetType()); 53 } 54 public void ShowDateTime(DateTime dt) 55 { 56 Console.WriteLine("ShowDateTime print {0},ShowDateTime Parament Type Is {1}", dt, dt.GetType()); 57 } 58 public void ShowPeople(People people) 59 { 60 Console.WriteLine("ShowPeople print {0},ShowPeople Parament Type Is {1}", people, people.GetType()); 61 } 62 } 63 64 public class People 65 { 66 public int Id { get; internal set; } 67 public string Name { get; internal set; } 68 }