常用的集合類型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.

只要物件類型實作 IEnumerableIEnumerable<T> 介面,LINQ to Objects 功能就可讓您使用 LINQ 查詢以存取記憶體內的物件。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 迴圈 (Loop) 更精簡、可讀性更高,並提供篩選、排序和群組功能。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).

標題Title 說明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 和 Dictionary 集合類型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>