ArrayList array = new ArrayList();
 array.Add("123");
 array.Add("test");
 array.Add(300);
 Console.WriteLine(array[2]);
View Code

这样一个非泛型集合对象的内容添加会经历一个装箱的过程,不知道添加的对象类型.

当输出Console.WriteLine(array[2]])的时候进行了拆箱操作.

所以从性能上面来说其实我们是可以避免掉此类情况发生的.

 List<int> list = new List<int>();
 list.Add(1);
 list.Add(2);
View Code

这样一个泛型集合对象的类型定义为int,在JIT中就不再需要进行装箱和拆箱操作了.

 

另外一个泛型和非泛型的区别就是对于类型的约束来说,泛型是基于类型安全来定义的.

 posted on 2016-09-08 10:44  joey  阅读(1857)  评论(0)    收藏  举报