何时使用泛型集合When to use generic collections

使用泛型集合可获得类型安全的自动化优点而无需从基集合类型派生和实现特定类型的成员。Using generic collections gives you the automatic 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's no need to box the elements.

对于面向 .NET Standard 1.0 或更高版本的程序,请在多个线程可能会同时向集合添加项或从集合中删除项时使用 System.Collections.Concurrent 命名空间中的泛型集合。For programs that target .NET Standard 1.0 or later, use the generic collection classes in the System.Collections.Concurrent namespace when multiple threads might be adding or removing items from the collection concurrently. 此外,当需要不可变性时,请考虑 System.Collections.Immutable 命名空间中的泛型集合类。Additionally, when immutability is desired, consider the generic collection classes in the System.Collections.Immutable namespace.

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

其他类型Additional Types

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

不可变生成器Immutable builders

如果需要在应用中使用不可变性功能,System.Collections.Immutable 命名空间提供了可使用的泛型集合类型。When you desire immutability functionality in your app, the System.Collections.Immutable namespace offers generic collection types you can use. 所有不可变的集合类型都提供 Builder 类,此类在执行多个突变时可以优化性能。All of the immutable collection types offer Builder classes that can optimize performance when you're performing multiple mutations. Builder 类在不可变状态下批处理操作。The Builder class batches operations in a mutable state. 所有突变都完成后,调用 ToImmutable 方法来“冻结”所有节点并创建一个不可变的泛型集合,例如 ImmutableList<T>When all mutations have been completed, call the ToImmutable method to "freeze" all nodes and create an immutable generic collection, for example, an ImmutableList<T>.

可以通过调用非泛型 CreateBuilder() 方法来创建 Builder 对象。The Builder object can be created by calling the nongeneric CreateBuilder() method. 通过 Builder 实例,可以调用 ToImmutable()From a Builder instance, you can call ToImmutable(). 同样,通过 Immutable* 集合中,可以调用 ToBuilder() 从泛型不可变集合创建生成器实例。Likewise, from the Immutable* collection, you can call ToBuilder() to create a builder instance from the generic immutable collection. 以下是各种 Builder 类型。The following are the various Builder types.

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)” 和 “并行 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