![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace _03泛型约束 7 { 8 9 class Student 10 { 11 12 } 13 class Program 14 { 15 static void Main(string[] args) 16 { 17 Person<Student, int, Car, Car, int, Student> p1 = new Person<Student, int, Car, Car, int, Student>(); 18 // p1.Age = "豆蔻"; 19 20 } 21 } 22 //where T1 : struct ,表示约束了类型T1必须是[值类型] 23 public class Person<T, T1, TC, TK, TV, TU> 24 where T1 : struct //约束T1必须是值类型 25 where T : class, new()//约束T必须是引用类型 26 where TC : new() //这个类型必须带有一个无参数的构造函数【要求:1.构造函数不能为私有,2.类型不能是抽象的。】 27 where TK : Car //这里约束了TK类型,必须是Car类型或者是Car类型的子类 28 where TV : IComparable //约束类TV必须是实现IComparable接口的类型。 29 where TU : T //约束了TU必须是T的子类。或者是T类型。 30 { 31 32 public T Name 33 { 34 get; 35 set; 36 } 37 public T1 Age 38 { 39 get; 40 set; 41 } 42 public T Email 43 { 44 get; 45 set; 46 } 47 48 public TC MyCar 49 { 50 get; 51 set; 52 } 53 54 } 55 public class Car 56 { 57 public Car() 58 { 59 this.Brand = "捷安特"; 60 } 61 public Car(string brand) 62 { 63 this.Brand = brand; 64 } 65 public string Brand 66 { 67 get; 68 set; 69 } 70 } 71 }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace _01自定义泛型 { class Program { static void Main(string[] args) { //Type Parameters //T: 其实就是一个占位符,将来用对应的类型来代替。 //TKey,TValue,TOutput,TReuslt // List<int> list = new List<int>(); //List< //list.Add( //Dictionary< //MyList ml = new MyList(); //Console.WriteLine(ml[4]); ////MyListString mls = new MyListString(); ////mls[0] = "yzk"; ////mls[1] = "sk"; MyListGeneric<int> mlg = new MyListGeneric<int>(100); mlg[0] = 10; mlg[1] = 101; mlg[2] = 102; Console.WriteLine(mlg[2]); Console.WriteLine("============================="); MyListGeneric<string> mlgString = new MyListGeneric<string>(100); mlgString[0] = "yzk"; mlgString[1] = "sk"; mlgString[2] = "jk"; Console.WriteLine(mlgString[1]); Console.WriteLine("========================="); Person p1 = new Person() { Name = "yp", Age = 100 }; Person p2 = new Person() { Name = "hf", Age = 109 }; MyListGeneric<Person> mlgPerson = new MyListGeneric<Person>(100); mlgPerson[0] = p1; mlgPerson[1] = p2; Console.WriteLine(mlgPerson[1].Name); Console.ReadKey(); } } public class Person { public string Name { get; set; } public int Age { get; set; } public string Email { get; set; } } class MyList { int[] arrInt = new int[] { 10, 20, 30, 40, 50, 60, 70 }; public int this[int index] { get { return arrInt[index]; } set { arrInt[index] = value; } } } class MyListString { string[] arrInt = new string[100]; public string this[int index] { get { return arrInt[index]; } set { arrInt[index] = value; } } } //泛型的目的也是为了代码重用(算法重用) //class MyListGeneric<T,K,V,TOut,T1> class MyListGeneric<T> { public MyListGeneric(int len) { arr = new T[len]; } //代码结构,算法一模一样,只是数据类型变了。 // T[] arr = new T[100]; private T[] arr; public T this[int index] { get { return arr[index]; } set { arr[index] = value; } } public void SayHi(T t) { Console.WriteLine(t); } } }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace _02泛型接口 7 { 8 class Program 9 { 10 static void Main(string[] args) 11 { 12 MyClass1<string> mc1 = new MyClass1<string>(); 13 //mc1.M1 14 Person p = new Person(); 15 //泛型方法,类本身不是泛型的,只有调用该方法的时候需要指定一下。 16 p.SayHi<string>("hi!!!!"); 17 Console.ReadKey(); 18 } 19 } 20 21 public interface ITest<T> 22 { 23 void M1(T t); 24 25 T M2(); 26 27 void M3(T obj); 28 29 T PValue 30 { 31 get; 32 set; 33 } 34 } 35 36 //实现接口时候的两种方式。 37 38 //“封闭类型” 39 class MyClass : ITest<string> 40 { 41 42 #region ITest<string> 成员 43 44 public void M1(string t) 45 { 46 throw new NotImplementedException(); 47 } 48 49 public string M2() 50 { 51 throw new NotImplementedException(); 52 } 53 54 public void M3(string obj) 55 { 56 throw new NotImplementedException(); 57 } 58 59 public string PValue 60 { 61 get 62 { 63 throw new NotImplementedException(); 64 } 65 set 66 { 67 throw new NotImplementedException(); 68 } 69 } 70 71 #endregion 72 } 73 74 //“开放类型” 75 class MyClass1<T> : ITest<T> 76 { 77 78 #region ITest<T> 成员 79 80 public void M1(T t) 81 { 82 throw new NotImplementedException(); 83 } 84 85 public T M2() 86 { 87 throw new NotImplementedException(); 88 } 89 90 public void M3(T obj) 91 { 92 throw new NotImplementedException(); 93 } 94 95 public T PValue 96 { 97 get 98 { 99 throw new NotImplementedException(); 100 } 101 set 102 { 103 throw new NotImplementedException(); 104 } 105 } 106 107 #endregion 108 } 109 110 111 112 //=====================泛型方法===================== 113 114 public class Person 115 { 116 public string Name 117 { 118 get; 119 set; 120 } 121 public int Age 122 { 123 get; 124 set; 125 } 126 public string Email 127 { 128 get; 129 set; 130 } 131 //泛型方法 132 public void SayHi<T>(T msg) 133 { 134 Console.WriteLine(msg); 135 } 136 137 } 138 }