何时使用泛型集合When to Use Generic Collections

通常建议使用泛型集合,因为这样你可以获得类型安全的直接优点而无需从基集合类型派生和实现特定类型的成员。Using generic collections is generally recommended, because you gain the immediate benefit of type safety without having to derive from a base collection type and implement type-specific members. 当集合元素为值类型时,泛型集合类型也通常优于对应的非泛型集合类型(比从非泛型基集合类型派生的类型好),因为使用泛型时不必对元素进行装箱。Generic collection types also generally perform better than the corresponding nongeneric collection types (and better than types that are derived from nongeneric base collection types) when the collection elements are value types, because with generics there is no need to box the elements.

对于面向 .NET Framework 4 或更高版本的程序,应在多个线程可能会同时向集合添加项或从集合中删除项时使用 System.Collections.Concurrent 命名空间中的泛型集合类。For programs that target the .NET Framework 4 or later, you should use the generic collection classes in the System.Collections.Concurrent namespace when multiple threads might be adding or removing items from the collection concurrently.

以下泛型类型对应于现有集合类型:The following generic types correspond to existing collection types:

其他类型Additional Types

几种泛型集合类型没有对应的非泛型集合类型。Several generic collection types do not have nongeneric counterparts. 它们包括以下类型:They include the following:

LINQ to ObjectsLINQ to Objects

你可以通过 LINQ to Objects 功能使用 LINQ 查询来访问内存中的对象,但条件是该对象类型要实现 System.Collections.IEnumerableSystem.Collections.Generic.IEnumerable<T> 接口。The LINQ to Objects feature enables you to use LINQ queries to access in-memory objects as long as the object type implements the System.Collections.IEnumerable or System.Collections.Generic.IEnumerable<T> interface. LINQ 查询提供了一种通用的数据访问模式;与标准 foreach 循环相比,它通常更加简洁,可读性更高;这种查询可提供筛选、排序和分组功能。LINQ queries provide a common pattern for accessing data; are typically more concise and readable than standard foreach loops; and provide filtering, ordering and grouping capabilities. LINQ 查询还可提高性能。LINQ queries can also improve performance. 有关详细信息,请参阅 LINQ to Objects (C#)LINQ to Objects (Visual Basic)Parallel LINQ (PLINQ)For more information, see LINQ to Objects (C#), LINQ to Objects (Visual Basic), and Parallel LINQ (PLINQ).

其他功能Additional Functionality

一些泛型类型具有非泛型集合类型中找不到的功能。Some of the generic types have functionality that is not found in the nongeneric collection types. 比如与非泛型 List<T> 类相对的 ArrayList 类有大量接受泛型委托的方法,例如允许你指定搜索列表的方法的 Predicate<T> 委托、代表对列表中每个元素发挥作用的 Action<T> 委托和在类型间定义对话的 Converter<TInput,TOutput> 委托。For example, the List<T> class, which corresponds to the nongeneric ArrayList class, has a number of methods that accept generic delegates, such as the Predicate<T> delegate that allows you to specify methods for searching the list, the Action<T> delegate that represents methods that act on each element of the list, and the Converter<TInput,TOutput> delegate that lets you define conversions between types.

List<T> 类使你可以指定你自己的用于排序和搜索列表的 IComparer<T> 泛型接口实现。The List<T> class allows you to specify your own IComparer<T> generic interface implementations for sorting and searching the list. SortedDictionary<TKey,TValue>SortedList<TKey,TValue> 类也有这个功能。The SortedDictionary<TKey,TValue> and SortedList<TKey,TValue> classes also have this capability. 另外,这些类使你可以在创建集合时指定比较器。In addition, these classes let you specify comparers when the collection is created. 同样地,Dictionary<TKey,TValue>KeyedCollection<TKey,TItem> 类让你指定自己的相等比较器。In similar fashion, the Dictionary<TKey,TValue> and KeyedCollection<TKey,TItem> classes let you specify your own equality comparers.

请参阅See also