"一次编码,多次使用",是引入泛型的根源。

  我们在编写程序时,经常遇到两个模块的功能非常相似,只是一个是处理int数据,另一个是处理string数据,或者其他自定义的数据类型,但我们没有办法,只能分别写多个方法处理每个数据类型,因为方法的参数类型不同。泛型的出现就是专门解决这个问题的,在方法中传入通用的数据类型,使代码可以进行合并。

通过创建泛型类,可以创建一个在编译时类型安全的集合。

ArrayList 是一个使用起来非常方便的集合类,无需进行修改即可用来存储任何引用或值类型。

// The .NET Framework 1.1 way to create a list:
System.Collections.ArrayList list1 = new System.Collections.ArrayList();
list1.Add(3);
list1.Add(105);

System.Collections.ArrayList list2 = new System.Collections.ArrayList();
list2.Add("It is raining in Redmond.");
list2.Add("It is snowing in the mountains.");

强制转换以及装箱和取消装箱操作都会降低性能;在必须对大型集合进行循环访问的情况下,装箱和取消装箱的影响非常明显。

  另一个限制是缺少编译时类型检查;因为 ArrayList 会将所有项都强制转换为 Object,所以在编译时无法防止客户端代码执行类似如下的操作:

System.Collections.ArrayList list = new System.Collections.ArrayList();
// Add an integer to the list.
list.Add(3);
// Add a string to the list. This will compile, but may cause an error later.
list.Add("It is raining in Redmond.");

int t = 0;
// This causes an InvalidCastException to be returned.
foreach (int x in list)
{
    t += x;
}

  尽管将字符串和 int 组合在一个 ArrayList 中的做法在创建异类集合时是完全可接受的,并且有时需要有意为之,但这种做法很可能产生编程错误,并且直到运行时才能检测到此错误。

当然,由于此类不可对多个数据类型重用,因此将丧失通用化的优点,并且您必须对要存储的每个类型重新编写该类。

List<T> 集合中,向集合添加项的操作类似于以下形式:

// The .NET Framework 2.0 way to create a list
List<int> list1 = new List<int>();

// No boxing, no casting:
list1.Add(3);

ArrayList 更安全并且速度更快的列表,对于列表项是值类型的情况尤为如此。

相关文章: