泛型优点: 1.性能 ArrayList list = new ArrayList(); list.Add(22); //装箱操作(值类型转引用类型),要装箱一个整数类型 int il = (int)list[0]; //拆箱操作(引用类型转值类型),把对象转换为整数类型 foreach (int i in list) { Response.Write(i); } /***** * 装箱,拆箱容易使用,但性能损失大,迭代许多时尤其如此 *****/ //List<T>类不使用对象,在使用时定义类型 //List<int>类的泛型类型定义为int,int类在编译器动态生成的类中使用,无需装箱,拆箱 List<int> list2 = new List<int>(); list2.Add(22); //不装箱 int il2 = list2[0]; //不拆箱 foreach (int i in list2) { Response.Write(i); } 2.类型安全 //如使用对象,可在集合中添加任意类型,不安全 ArrayList list3 = new ArrayList(); list3.Add(22); list3.Add("hello"); list3.Add(new Car()); //使用foreach语句迭代上集合,使用整数类型迭代,编译器可编译,但运行报异常 foreach (int i in list3) { Response.Write(i); } List<int> list4 = new List<int>(); list4.Add(22); //list4.Add("hello"); 编译报错 //list4.Add(new Car()); 编译报错 3.二进制代码重用 List<int> list5 = new List<int>(); list5.Add(22); List<Car> myCarList = new List<Car>(); myCarList.Add(new Car()); 4.代码扩展 5.命名约定 泛型类命名规则: 泛型类名称用字母T为前缀