.NET 中的泛型集合Generic collections in .NET

.NET 类库提供了许多 System.Collections.GenericSystem.Collections.ObjectModel 命名空间中的泛型集合类。The .NET class library provides a number of generic collection classes in the System.Collections.Generic and System.Collections.ObjectModel namespaces. 若要详细了解这些类,请参阅常用集合类型For more detailed information about these classes, see Commonly Used Collection Types.

System.Collections.GenericSystem.Collections.Generic

许多泛型集合类型均为非泛型类型的直接模拟。Many of the generic collection types are direct analogs of nongeneric types. Dictionary<TKey,TValue>Hashtable 的泛型版本;它使用枚举的泛型结构 KeyValuePair<TKey,TValue> 而不是 DictionaryEntryDictionary<TKey,TValue> is a generic version of Hashtable; it uses the generic structure KeyValuePair<TKey,TValue> for enumeration instead of DictionaryEntry.

List<T>ArrayList 的泛型版本。List<T> is a generic version of ArrayList. 存在响应非泛型版本的泛型 Queue<T>Stack<T> 类。There are generic Queue<T> and Stack<T> classes that correspond to the nongeneric versions.

存在 SortedList<TKey,TValue> 的泛型和非泛型版本。There are generic and nongeneric versions of SortedList<TKey,TValue>. 这两个版本均为字典和列表的混合。Both versions are hybrids of a dictionary and a list. SortedDictionary<TKey,TValue> 泛型类是一个纯字典,并且没有任何非泛型对应项。The SortedDictionary<TKey,TValue> generic class is a pure dictionary and has no nongeneric counterpart.

LinkedList<T> 泛型类是真正的链接列表。The LinkedList<T> generic class is a true linked list. 它没有任何非泛型对应项。It has no nongeneric counterpart.

System.Collections.ObjectModelSystem.Collections.ObjectModel

Collection<T> 泛型类提供用于派生自己的泛型集合类型的基类。The Collection<T> generic class provides a base class for deriving your own generic collection types. ReadOnlyCollection<T> 类提供了任何从实现 IList<T> 泛型接口的类型生成只读集合的简便方法。The ReadOnlyCollection<T> class provides an easy way to produce a read-only collection from any type that implements the IList<T> generic interface. KeyedCollection<TKey,TItem> 泛型类提供了存储包含其自己的键的对象的方法。The KeyedCollection<TKey,TItem> generic class provides a way to store objects that contain their own keys.

其他泛型类型Other generic types

Nullable<T> 泛型结构允许使用值类型,如同它们可分配 nullThe Nullable<T> generic structure allows you to use value types as if they could be assigned null. 这在处理数据库查询时很有用,其中字段包含可能丢失的值类型。This can be useful when working with database queries, where fields that contain value types can be missing. 泛型类型参数可为任意值类型。The generic type parameter can be any value type.

备注

在 C# 和 Visual Basic 中,无需显式使用 Nullable<T>,因为语言具有可以为 null 类型的语法。In C# and Visual Basic, it is not necessary to use Nullable<T> explicitly because the language has syntax for nullable types. 请参阅可为 null 的值类型(C# 引用)可为 null 的值类型 (Visual Basic)See Nullable value types (C# reference) and Nullable value types (Visual Basic).

ArraySegment<T> 泛型结构提供了分隔任何类型的从零开始的一维数组内的一系列元素的方法。The ArraySegment<T> generic structure provides a way to delimit a range of elements within a one-dimensional, zero-based array of any type. 泛型类型参数是数组中元素的类型。The generic type parameter is the type of the array's elements.

如果你的事件采用 .NET 所使用的事件处理模式,则 EventHandler<TEventArgs> 泛型委托不需要声明委托类型来处理事件。The EventHandler<TEventArgs> generic delegate eliminates the need to declare a delegate type to handle events, if your event follows the event-handling pattern used by .NET. 例如,假设已创建从 EventArgs 派生的 MyEventArgs 类,以包含事件的数据。For example, suppose you have created a MyEventArgs class, derived from EventArgs, to hold the data for your event. 则可以声明此事件,如下所示:You can then declare the event as follows:

public:
    event EventHandler<MyEventArgs^>^ MyEvent;
public event EventHandler<MyEventArgs> MyEvent;
Public Event MyEvent As EventHandler(Of MyEventArgs)

请参阅See also