以下是 .NET 类库提供的主要数据结构分类列表(基于最新稳定版本),涵盖集合、线性结构、树形结构、图结构等:
一、线性集合(顺序存储)
类型 |
描述 |
时间复杂度 |
List<T> |
动态数组,自动扩容 |
访问: O(1) 插入/删除: O(n) |
LinkedList<T> |
双向链表 |
插入/删除: O(1) 访问: O(n) |
Queue<T> |
先进先出(FIFO)队列 |
入队/出队: O(1) |
Stack<T> |
后进先出(LIFO)栈 |
入栈/出栈: O(1) |
ImmutableList<T> |
不可变列表 |
修改操作返回新实例 |
二、键值对集合(关联存储)
类型 |
描述 |
实现方式 |
Dictionary<TKey, TValue> |
哈希表实现的键值对 |
哈希表 |
SortedDictionary<TKey, TValue> |
基于二叉搜索树的有序字典 |
红黑树 |
ConcurrentDictionary<TKey, TValue> |
线程安全的字典 |
分段锁哈希表 |
ImmutableDictionary<TKey, TValue> |
不可变字典 |
哈希树 |
Hashtable (非泛型) |
早期非泛型键值集合 |
哈希表 |
HybridDictionary |
小数据用List,大数据转Hashtable |
混合实现 |
三、特殊集合
类型 |
描述 |
典型用途 |
HashSet<T> |
不重复值的哈希集合 |
去重/集合运算 |
SortedSet<T> |
有序不重复集合 |
范围查询 |
BitArray |
位操作集合 |
紧凑存储布尔值 |
ObservableCollection<T> |
可通知变化的集合 |
WPF数据绑定 |
BlockingCollection<T> |
线程安全的生产者-消费者模型 |
并发队列 |
四、多维/复杂结构
类型 |
描述 |
Array (所有数组) |
基础的多维数组 |
ArraySegment<T> |
数组片段 |
Memory<T> /Span<T>
|
高性能内存视图 |
DataTable |
内存关系表(ADO.NET) |
五、并发数据结构(System.Collections.Concurrent)
类型 |
描述 |
ConcurrentQueue<T> |
线程安全队列 |
ConcurrentStack<T> |
线程安全栈 |
ConcurrentBag<T> |
无序线程安全集合 |
Partitioner<T> |
并行任务数据分区工具 |
六、不可变集合(System.Collections.Immutable)
类型 |
描述 |
ImmutableArray<T> |
高性能不可变数组 |
ImmutableQueue<T> |
不可变队列 |
ImmutableStack<T> |
不可变栈 |
ImmutableHashSet<T> |
不可变哈希集合 |
七、遗留集合(非泛型,不推荐新项目使用)
类型 |
替代方案 |
ArrayList |
List<T> |
Queue |
Queue<T> |
Stack |
Stack<T> |
SortedList |
SortedDictionary<TKey,TValue> |
八、选择指南
-
需要快速查找 →
Dictionary<TKey, TValue>
-
需要有序遍历 →
SortedDictionary<TKey, TValue>
-
线程安全需求 →
ConcurrentDictionary<TKey, TValue>
-
高性能只读场景 →
ImmutableArray<T>
-
先进先出处理 →
Queue<T>
或 ConcurrentQueue<T>
九、性能关键点
-
内存连续性:
List<T>
和 Array
对CPU缓存友好
-
哈希冲突:
Dictionary
的扩容开销(默认容量为素数)
-
树结构平衡:
SortedDictionary
使用红黑树保证操作稳定在 O(log n)
所有类型均位于 System.Collections
或 System.Collections.Generic
命名空间,需要时添加对应 using 指令。