常用的集合类型Commonly Used Collection Types

集合类型是数据集合(如哈希表、队列、堆栈、包、字典和列表)的常见变体。Collection types are the common variations of data collections, such as hash tables, queues, stacks, bags, dictionaries, and lists.

集合基于 ICollection 接口、 IList 接口、IDictionary 接口或它们对应的泛型集合。Collections are based on the ICollection interface, the IList interface, the IDictionary interface, or their generic counterparts. IList 接口和 IDictionary 接口都派生自 ICollection 接口:因此,所有集合都直接或间接基于 ICollection 接口。The IList interface and the IDictionary interface are both derived from the ICollection interface; therefore, all collections are based on the ICollection interface either directly or indirectly. 在基于 IList 接口(比如 ArrayArrayListList<T>)或直接基于 ICollection 接口(比如 QueueConcurrentQueue<T>StackConcurrentStack<T>LinkedList<T>)的集合里,每个元素都只有一个值。In collections based on the IList interface (such as Array, ArrayList, or List<T>) or directly on the ICollection interface (such as Queue, ConcurrentQueue<T>, Stack, ConcurrentStack<T> or LinkedList<T>), every element contains only a value. 在基于 IDictionary 接口(比如 HashtableSortedList 类,Dictionary<TKey,TValue>SortedList<TKey,TValue> 泛型类)或 ConcurrentDictionary<TKey,TValue> 类的集合中,每个元素都有一个键和一个值。In collections based on the IDictionary interface (such as the Hashtable and SortedList classes, the Dictionary<TKey,TValue> and SortedList<TKey,TValue> generic classes), or the ConcurrentDictionary<TKey,TValue> classes, every element contains both a key and a value. KeyedCollection<TKey,TItem> 类是唯一的,因为它是值中嵌键的值的列表,因此,它的行为类似列表和字典。The KeyedCollection<TKey,TItem> class is unique because it is a list of values with keys embedded within the values and, therefore, it behaves like a list and like a dictionary.

泛型集合都是强类型的最佳解决方案。Generic collections are the best solution to strong typing. 但,如果你的语言不支持泛型,那么 System.Collections 命名空间包含基集合,如 CollectionBaseReadOnlyCollectionBaseDictionaryBase,这些集合都是可扩展以创建强类型集合类的抽象基类。However, if your language does not support generics, the System.Collections namespace includes base collections, such as CollectionBase, ReadOnlyCollectionBase, and DictionaryBase, which are abstract base classes that can be extended to create collection classes that are strongly typed. 需要高效的多线程集合访问时,请使用 System.Collections.Concurrent 命名空间中的泛型集合。When efficient multi-threaded collection access is required, use the generic collections in the System.Collections.Concurrent namespace.

集合会因元素的存储方式、排序方式、执行搜索的方式以及比较方式的不同而不同。Collections can vary, depending on how the elements are stored, how they are sorted, how searches are performed, and how comparisons are made. Queue 类和 Queue<T> 泛型类提供先进先出列表,而 Stack 类和 Stack<T> 泛型类提供后进先出列表。The Queue class and the Queue<T> generic class provide first-in-first-out lists, while the Stack class and the Stack<T> generic class provide last-in-first-out lists. SortedList 类和 SortedList<TKey,TValue> 泛型类提供 Hashtable 类和 Dictionary<TKey,TValue> 泛型类的已排序版本。The SortedList class and the SortedList<TKey,TValue> generic class provide sorted versions of the Hashtable class and the Dictionary<TKey,TValue> generic class. HashtableDictionary<TKey,TValue> 的元素只能通过元素的键访问,但 SortedListKeyedCollection<TKey,TItem> 的元素能通过元素的键或索引访问。The elements of a Hashtable or a Dictionary<TKey,TValue> are accessible only by the key of the element, but the elements of a SortedList or a KeyedCollection<TKey,TItem> are accessible either by the key or by the index of the element. 所有集合中的索引都从零开始,Array 除外,它允许不从零开始的数组。The indexes in all collections are zero-based, except Array, which allows arrays that are not zero-based.

LINQ to Objects 功能让你可以通过使用 LINQ 查询来访问内存中的对象,条件是该对象类型实现 IEnumerableIEnumerable<T>The LINQ to Objects feature allows you to use LINQ queries to access in-memory objects as long as the object type implements IEnumerable or IEnumerable<T>. 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).

TitleTitle 说明Description
集合和数据结构Collections and Data Structures 讨论在 .NET Framework 中提供的各种集合类型,包括堆栈、队列、列表、数组和字典。Discusses the various collection types available in the .NET Framework, including stacks, queues, lists, arrays, and dictionaries.
哈希表和字典集合类型Hashtable and Dictionary Collection Types 描述泛型和非泛型的基于哈希的字典类型的功能。Describes the features of generic and nongeneric hash-based dictionary types.
已排序的集合类型Sorted Collection Types 描述为列表和集提供排序功能的类。Describes classes that provide sorting functionality for lists and sets.
泛型Generics 描述泛型功能,包括 .NET Framework 提供的泛型集合、委托和接口。Describes the generics feature, including the generic collections, delegates, and interfaces provided by the .NET Framework. 为 C#、Visual Basic 和 Visual C++ 提供功能文档链接和支持技术(如反射)链接。Provides links to feature documentation for C#, Visual Basic, and Visual C++, and to supporting technologies such as reflection.

参考Reference

System.Collections

System.Collections.Generic

System.Collections.ICollection

System.Collections.Generic.ICollection<T>

System.Collections.IList

System.Collections.Generic.IList<T>

System.Collections.IDictionary

System.Collections.Generic.IDictionary<TKey,TValue>