@IT·互联网

C#.NET 集合框架详解

2025-07-14  本文已影响0人  青枫教学

简介

C# 集合框架是处理数据集合的核心组件,位于 System.CollectionsSystem.Collections.Generic 命名空间。它提供了多种数据结构来高效存储和操作数据。

集合框架概览

System.Collections               (非泛型老版)
└─ System.Collections.Generic    (泛型现代版)
   ├─ IList<T> ← List<T>, LinkedList<T>, ObservableCollection<T>, …
   ├─ ICollection<T> ← HashSet<T>, SortedSet<T>, …
   ├─ IDictionary<TKey,TValue> ← Dictionary<TKey,TValue>, SortedDictionary<,>, …
   └─ IEnumerable<T>
System.Collections.Concurrent    (并发安全集合)
System.Collections.Immutable     (不可变集合)
System.Collections.ObjectModel   (通知模型集合)

核心接口

接口 功能
IEnumerable<T> 可枚举,支持 foreach
ICollection<T> : IEnumerable<T> 增删改查,CountAddRemoveContains
IList<T> : ICollection<T> 支持按索引访问,InsertIndexOf
IDictionary<TKey,TValue> 键/值对,this[key]TryGetValue
IReadOnlyCollection<T> 只读视图
IReadOnlyList<T> 只读索引
IReadOnlyDictionary<,> 只读字典

主要实现与特性

顺序访问集合

类型 特点 复杂度(N=Count)
Array 固定大小,按索引快速访问 读/写 O(1)
List<T> 可变大小的数组;增长时可能触发内部扩容 读 O(1),插尾 O(1) 摊销;插中间 O(N)
LinkedList<T> 双向链表,插入/删除任意节点 O(1)(已知节点);按索引 O(N) 访问 O(N),插/删节点 O(1)
ObservableCollection<T> 在 UI/MVVM 中使用,修改时触发事件 List<T>

键/值对集合

类型 特点 复杂度
Dictionary<TKey,TValue> 哈希表,平均 O(1) 查找/添加/删除 查找/添加/删除 O(1)
SortedDictionary<TKey,TValue> 基于红黑树,键有序,O(log N) 操作 查找/添加/删除 O(log N)
SortedList<TKey,TValue> 底层数组,按索引访问快,插入/删除 O(N) 访问 O(log N) 二分查找;插/删 O(N)

集合语义的集合

类型 特点 复杂度
HashSet<T> 无序、不重复元素,基于哈希 查找/添加/删除 O(1)
SortedSet<T> 有序、不重复,基于红黑树 查找/添加/删除 O(log N)
Queue<T> 先进先出,基于循环数组 入队/出队 O(1)
Stack<T> 后进先出,基于数组 压栈/弹栈 O(1)
BlockingCollection<T> 有界/无界并发队列,支持阻塞和取消 入/出队 O(1)

并发集合(System.Collections.Concurrent)

类型 特点 场景
ConcurrentDictionary<,> 线程安全的字典,分段锁或自旋锁 高并发读写字典
ConcurrentQueue<T> 多生产者/多消费者队列 并发任务调度
ConcurrentStack<T> 并发栈 并发回退/撤销操作
ConcurrentBag<T> 无序的线程本地存储集合 并发任务结果收集
BlockingCollection<T> 封装多个并发集合,支持生产者/消费者模式 流水线计算

不可变集合(System.Collections.Immutable)

var list1 = ImmutableList<int>.Empty;
var list2 = list1.Add(1);

性能选型

常见陷阱

常用集合详解

List<T> - 动态数组

// 创建与初始化
var numbers = new List<int> { 1, 2, 3 };

// 添加元素
numbers.Add(4);
numbers.AddRange(new[] { 5, 6 });

// 插入元素
numbers.Insert(0, 0); // 开头插入

// 删除元素
numbers.Remove(3);    // 按值删除
numbers.RemoveAt(0);  // 按索引删除

// 容量优化(减少内存占用)
numbers.TrimExcess();

// 预分配容量(提升性能)
var largeList = new List<int>(capacity: 1000);

Dictionary<K,V> - 哈希字典

var users = new Dictionary<int, string>
{
    [1] = "Alice",
    [2] = "Bob"
};

// 安全访问
if (users.TryGetValue(2, out string name))
{
    Console.WriteLine(name); // 输出 "Bob"
}

// 添加或更新
users[3] = "Charlie"; // 添加
users[2] = "Robert";  // 更新

// 遍历
foreach (var kvp in users)
{
    Console.WriteLine($"ID: {kvp.Key}, Name: {kvp.Value}");
}

// 自定义键类型(需实现GetHashCode和Equals)
public record UserId(int Id);
var userDict = new Dictionary<UserId, string>();

HashSet<T> - 唯一值集合

var set1 = new HashSet<int> { 1, 2, 3, 4 };
var set2 = new HashSet<int> { 3, 4, 5, 6 };

// 集合运算
set1.UnionWith(set2);      // 并集: {1,2,3,4,5,6}
set1.IntersectWith(set2);   // 交集: {3,4}
set1.ExceptWith(set2);      // 差集: {1,2}

// 存在性检查
bool contains = set1.Contains(3); // true

SortedSet<T> - 有序唯一集合

SortedSet<int> sortedSet = new SortedSet<int> { 3, 1, 2 };
// sortedSet 元素顺序:1, 2, 3

int min = sortedSet.Min;  // 最小元素:1
int max = sortedSet.Max;  // 最大元素:3

Queue<T> - 先进先出队列

var queue = new Queue<string>();
queue.Enqueue("First");
queue.Enqueue("Second");
queue.Enqueue("Third");

while (queue.Count > 0)
{
    string item = queue.Dequeue();
    Console.WriteLine(item); // 输出 First → Second → Third
}

Stack<T> - 后进先出栈

var stack = new Stack<int>();
stack.Push(1);
stack.Push(2);
stack.Push(3);

while (stack.Count > 0)
{
    int top = stack.Pop();
    Console.WriteLine(top); // 输出 3 → 2 → 1
}

LinkedList<T> - 双向链表

var list = new LinkedList<string>();
var node1 = list.AddFirst("First");
var node3 = list.AddLast("Third");
var node2 = list.AddAfter(node1, "Second");

list.Remove(node2);  // 高效删除中间节点

// 顺序遍历
var current = list.First;
while (current != null)
{
    Console.WriteLine(current.Value);
    current = current.Next;
}

SortedDictionary<K,V>SortedList<K,V>

并发集合(System.Collections.Concurrent)

ConcurrentDictionary<K,V>

var concurrentDict = new ConcurrentDictionary<int, string>();
concurrentDict.TryAdd(1, "One");
concurrentDict.AddOrUpdate(1, k => "New", (k, v) => "Updated");

ConcurrentQueue<T>

var queue = new ConcurrentQueue<int>();
queue.Enqueue(1);
if (queue.TryDequeue(out int result)) { ... }

ConcurrentBag<T>

无序集合,适用于生产者-消费者场景

var bag = new ConcurrentBag<int>();
bag.Add(42);
if (bag.TryTake(out int item)) { ... }

BlockingCollection - 生产者消费者模型

BlockingCollection<int> buffer = new BlockingCollection<int>(boundedCapacity: 5);

// 生产者
Task.Run(() => {
    for (int i = 0; i < 10; i++) {
        buffer.Add(i);
        Thread.Sleep(100);
    }
    buffer.CompleteAdding();
});

// 消费者
foreach (int item in buffer.GetConsumingEnumerable()) {
    Console.WriteLine(item);
}

不可变集合(System.Collections.Immutable)

// 创建不可变集合
var immutableList = ImmutableList.Create(1, 2, 3);

// 所有"修改"操作返回新集合
var newList = immutableList.Add(4).Remove(2);

// 原集合保持不变
Console.WriteLine(immutableList.Count); // 输出 3
Console.WriteLine(newList.Count);       // 输出 3 (添加1个移除1个)

内存安全集合

性能特点:

var builder = ImmutableArray.CreateBuilder<int>();
builder.Add(1);
builder.Add(2);
ImmutableArray<int> immutable = builder.ToImmutable();

// 修改操作返回新实例
ImmutableArray<int> newArray = immutable.Add(3);

性能优化技巧

预分配容量:对已知大小的集合初始化时指定容量

var list = new List<int>(1000); // 避免多次扩容

避免装箱拆箱:优先使用泛型集合而非非泛型集合

// 推荐
List<int> intList = new();

// 避免
ArrayList oldList = new(); // 导致装箱

批量操作:使用 AddRange() 替代循环中的单个 Add()

// 高效方式
list.AddRange(items);

// 低效方式
foreach (var item in items) list.Add(item);

选择合适相等比较器:自定义字典键时实现 IEquatable<T> 和重写 GetHashCode()

public class CustomKey : IEquatable<CustomKey>
{
    public int Id { get; set; }

    public bool Equals(CustomKey other) => Id == other?.Id;
    
    public override int GetHashCode() => Id.GetHashCode();
}

枚举优化:避免在热路径中创建枚举器

// 低效
for (int i = 0; i < list.Count; i++) { ... }

// 高效
int count = list.Count;
for (int i = 0; i < count; i++) { ... }

接口返回只读视图

public IReadOnlyList<string> GetItems()
{
    return _internalList.AsReadOnly();
}

使用集合初始化器:简洁的初始化语法

var dict = new Dictionary<int, string>
{
    [1] = "One",
    [2] = "Two"
};

LINQ结合使用:利用LINQ进行复杂查询

var recentOrders = orders
    .Where(o => o.Date > DateTime.Now.AddDays(-7))
    .OrderByDescending(o => o.Total)
    .ToList();

避免修改遍历中的集合:使用 ToList() 创建副本

foreach (var item in collection.ToList())
{
    if (condition) collection.Remove(item);
}

实践场景

使用 Try 方法

使用 TryGetValue、TryAdd 提高效率

if (dict.TryGetValue(key, out var value))
{
    // 使用 value
}

相等性实现

public class Person : IEquatable<Person>
{
    public int Id { get; set; }
    
    public bool Equals(Person? other) => other?.Id == Id;
    
    public override int GetHashCode() => Id.GetHashCode();
}

// 自定义比较器
class CaseInsensitiveComparer : IEqualityComparer<string>
{
    public bool Equals(string? x, string? y) 
        => string.Equals(x, y, StringComparison.OrdinalIgnoreCase);
    
    public int GetHashCode(string obj) 
        => obj.ToUpperInvariant().GetHashCode();
}

枚举安全

// 错误:在枚举中修改集合
foreach (var item in list) {
    if (item.ShouldRemove) 
        list.Remove(item); // 抛出InvalidOperationException
}

// 正确:使用临时集合
List<Item> toRemove = list.Where(item => item.ShouldRemove).ToList();
foreach (var item in toRemove) {
    list.Remove(item);
}

值类型集合优化

// 避免装箱
List<Point> points = new List<Point>(); // 结构体集合
points.Add(new Point(1, 2));

// 优先考虑Span<T>处理内存连续数据
Span<int> span = CollectionsMarshal.AsSpan(numbers);

LRU缓存实现

public class LRUCache<TKey, TValue>
{
    private readonly Dictionary<TKey, LinkedListNode<CacheItem>> _dict;
    private readonly LinkedList<CacheItem> _list;
    private readonly int _capacity;

    public LRUCache(int capacity)
    {
        _capacity = capacity;
        _dict = new Dictionary<TKey, LinkedListNode<CacheItem>>(capacity);
        _list = new LinkedList<CacheItem>();
    }

    public TValue Get(TKey key)
    {
        if (_dict.TryGetValue(key, out var node))
        {
            _list.Remove(node);
            _list.AddFirst(node);
            return node.Value.Value;
        }
        return default;
    }

    public void Add(TKey key, TValue value)
    {
        if (_dict.Count >= _capacity)
        {
            var last = _list.Last;
            _dict.Remove(last.Value.Key);
            _list.RemoveLast();
        }

        var newNode = new LinkedListNode<CacheItem>(
            new CacheItem { Key = key, Value = value });
        
        _list.AddFirst(newNode);
        _dict.Add(key, newNode);
    }

    private class CacheItem
    {
        public TKey Key { get; init; }
        public TValue Value { get; init; }
    }
}

数据批处理

const int BatchSize = 100;
List<Data> batch = new List<Data>(BatchSize);

foreach (var item in massiveCollection)
{
    batch.Add(item);
    
    if (batch.Count == BatchSize)
    {
        ProcessBatch(batch);
        batch.Clear();
    }
}

ProcessBatch(batch); // 处理剩余项
上一篇 下一篇

猜你喜欢

热点阅读