集合分為兩種:非泛型集合,泛型集合。
非泛型集合需要引入:System.Collections命名空間,其命名空間下的類有:
ArrayList表示大小根據需要動態增加的對象數組。
Hashtable表示根據鍵的哈希代碼進行組織的鍵/值對的集合。
Queue表示對象的先進先出(FIFO)集合。
Stack表示對象的后進先出(LIFO)集合。
Stack stack=new Stack();
Stack<int> stack=new Stack<int>();
泛型集合需要引入:System.Collection.Generic命名空間,其命名空間下的類有:
Dictionary<TKey,TValue>表示根據鍵進行組織的鍵/值對的集合。
Dictionary<string,int> dic=new Dictionary<string,int>();
List<T>表示可根據索引訪問對象的列表。提供用於對列表進行搜索,排序和修改的方法
Queue<T>表示對象的先進先出集合
SortedList<TKye,TValue>
表示根據鍵進行排序的鍵/值對的集合,而鍵基於的是相關 IComparer<T>實現
Stack<T>表示對象的后進先出集合
我要…… |
泛型集合選項 |
非泛型集合選項 |
線程安全或不可變集合選項 |
將項存儲為鍵/值對以通過鍵進行快速查找 |
Dictionary<TKey, TValue> |
Hashtable (根據鍵的哈希代碼組織的鍵/值對的集合。) |
System.Collections.Concurrent.ConcurrentDictionary<TKey, TValue> System.Collections.ObjectModel.ReadOnlyDictionary<TKey, TValue> ImmutableDictionary(TKey, TValue) 類 |
按索引訪問項 |
List<T> |
System.Array System.Collections.ArrayList |
ImmutableList(T) 類 ImmutableArray 類 |
使用項先進先出 (FIFO) |
Queue<T> |
Queue |
System.Collections.Concurrent.ConcurrentQueue<T> ImmutableQueue(T) 類 |
使用數據后進先出 (LIFO) |
Stack<T> |
Stack |
System.Collections.Concurrent.ConcurrentStack<T> ImmutableStack(T) 類 |
按順序訪問項 |
LinkedList<T> |
無建議 |
無建議 |
刪除集合中的項或向集合添加項時接收通知。 (實現 INotifyPropertyChanged 和 System.Collections.Specialized.INotifyCollectionChanged) |
System.Collections.ObjectModel.ObservableCollection<T> |
無建議 |
無建議 |
已排序的集合 |
System.Collections.Generic.SortedList<TKey, TValue> |
System.Collections.SortedList |
ImmutableSortedDictionary(TKey, TValue) 類 ImmutableSortedSet(T) 類 |
數學函數的一個集 |
System.Collections.Generic.HashSet<T> System.Collections.Generic.SortedSet<T> |
無建議 |
ImmutableHashSet(T) 類 ImmutableSortedSet(T) 類 |