在 C# 中,除了 Dictionary
、List
、HashSet
和 Hashtable
之外,還有許多其他可以保存列表或集合類型的數據結構,具體包括以下幾類:
📌 數組類
1. Array(數組)
- 固定長度,性能高,適用于已知大小的數據集。
- 適合頻繁訪問但不需要修改大小的場景。
csharp
int[] arr = new int[5] { 1, 2, 3, 4, 5 };
📌 列表類
2. Queue<T>(隊列)
- 先進先出(FIFO)結構。
- 適用于按順序處理任務的場景。
csharp
Queue<int> queue = new Queue<int>(); queue.Enqueue(1); int item = queue.Dequeue(); // 取出第一個元素
3. Stack<T>(棧)
- 后進先出(LIFO)結構。
- 適用于逆序或撤銷操作的場景。
csharp
Stack<int> stack = new Stack<int>(); stack.Push(1); int item = stack.Pop(); // 取出最后一個元素
4. LinkedList<T>(鏈表)
- 雙向鏈表,支持在頭尾或中間快速插入和刪除操作。
- 適用于頻繁插入和刪除的場景。
csharp
LinkedList<int> list = new LinkedList<int>(); list.AddFirst(1); list.AddLast(2);
📌 集合類
5. SortedSet<T>(有序集合)
- 保證元素唯一性,并且自動按升序排序。
csharp
SortedSet<int> set = new SortedSet<int>(); set.Add(3); set.Add(1); set.Add(2); // 結果:1, 2, 3(自動排序)
6. ConcurrentBag<T>(線程安全集合)
- 允許并發訪問的集合,適用于多線程場景。
csharp
ConcurrentBag<int> bag = new ConcurrentBag<int>(); bag.Add(1); bag.Add(2);
7. BlockingCollection<T>(阻塞集合)
- 提供線程安全的生產者/消費者模式。
csharp
BlockingCollection<int> collection = new BlockingCollection<int>(); collection.Add(1); int item = collection.Take(); // 阻塞直到有數據
8. ObservableCollection<T>(可觀察集合)
- 當集合發生變化時會觸發通知(通常用于數據綁定)。
csharp
ObservableCollection<int> observableList = new ObservableCollection<int>();
observableList.CollectionChanged += (sender, e) =>
{ Console.WriteLine("Collection changed");
};
observableList.Add(1);
📌 映射類
9. SortedDictionary<TKey, TValue>(有序字典)
- 按鍵的升序排序,基于
Red-Black Tree
實現。
csharp
SortedDictionary<int, string> dict = new SortedDictionary<int, string>();
dict.Add(2, "B");
dict.Add(1, "A");
dict.Add(3, "C"); // 輸出順序為:1 -> 2 -> 3
10. SortedList<TKey, TValue>(有序列表)
- 基于數組,按鍵排序,插入和刪除速度較慢。
csharp
SortedList<int, string> sortedList = new SortedList<int, string>();
sortedList.Add(1, "A");
sortedList.Add(2, "B");
11. ConcurrentDictionary<TKey, TValue>(線程安全字典)
- 線程安全的鍵值對集合,適合在多線程場景下使用。
csharp
ConcurrentDictionary<int, string> dict = new ConcurrentDictionary<int, string>();
dict.TryAdd(1, "A");
📌 特殊集合類
12. BitArray(位數組)
- 用于高效存儲和操作位(布爾值)。
csharp
BitArray bits = new BitArray(8);
bits[0] = true;
13. NameValueCollection(鍵值對集合,允許重復鍵)
- 允許鍵重復,存儲字符串鍵值對。
csharp
NameValueCollection collection = new NameValueCollection();
collection.Add("key", "value1");
collection.Add("key", "value2");
14. HybridDictionary(小規模時使用 ListDictionary
,大規模時自動切換到 Hashtable
)
- 在數據量少時使用
ListDictionary
,大時切換為Hashtable
。
csharp
HybridDictionary hybridDict = new HybridDictionary();
hybridDict.Add("key", "value");
15. ImmutableArray<T>, ImmutableList<T>, ImmutableDictionary<TKey, TValue>(不可變集合)
- 定義后不可修改,適用于線程安全場景。
csharp
var immutableList = ImmutableList.Create(1, 2, 3);
immutableList = immutableList.Add(4);
🔥 總結
數據結構 | 特點 | 適用場景 |
---|---|---|
Array | 固定大小,訪問快 | 固定長度數據集 |
List<T> | 可變長度,支持索引訪問 | 隨機訪問和動態添加 |
LinkedList<T> | 雙向鏈表,插入/刪除快 | 頻繁修改和插入 |
Stack<T> | 后進先出 | 逆序操作 |
Queue<T> | 先進先出 | 按順序處理任務 |
HashSet<T> | 元素唯一 | 去重集合 |
SortedSet<T> | 唯一且排序 | 唯一+排序 |
Dictionary<K,V> | 快速鍵值對訪問 | 快速查找 |
SortedDictionary<K,V> | 按鍵排序 | 排序+快速查找 |
ConcurrentBag<T> | 線程安全的集合 | 并發訪問 |
ImmutableList<T> | 不可變集合 | 線程安全 |
如果你要在多線程環境下操作,建議用 ConcurrentDictionary
、ConcurrentBag
或 BlockingCollection
。
如果需要有序性,用 SortedList
或 SortedDictionary
。
如果要去重,用 HashSet
或 SortedSet
。