C#泛型
看到泛型都感觉无从入手,其实不然..
泛型指的的泛指的类型,话句话说就是类型可以是任意的,但一旦确定了某一种类型之后,那么类型确定,不能在换之。 List<T> :可以把T换成任何的类型。从而减少了装箱和拆箱的操作。
定义一个泛型类。
看段练习吧...
View Code
1 using System; 2 public class studyGeneric 3 { 4 public static void Main() 5 { 6 People<string,int> p = new People<string,int>("张三",25); 7 People<int,string> p1 = new People<int,string>(23,"lishi"); 8 Console.WriteLine(p); 9 Console.WriteLine(p1); 10 } 11 } 12 public class People<S,I> 13 { 14 private I age ; 15 public I Age 16 { 17 get{return this.age;} 18 set{this.age = value;} 19 } 20 public S Name 21 { 22 get;set; 23 } 24 public People(){} 25 public People(S s,I i) 26 { 27 this.Name = s; 28 this.age = i; 29 } 30 }