C# 集合

2024年8月29日 | 1 分钟阅读

在 C# 中,集合表示对象的组。通过集合,我们可以对对象执行各种操作,例如

  • 存储对象
  • 更新对象
  • 删除对象
  • 检索对象
  • 搜索对象,以及
  • 排序对象

在排序方面,所有数据结构都可以通过 C# 集合来完成。

我们可以将对象存储在数组或集合中。集合相对于数组具有优势。数组的大小有限,但存储在集合中的对象可以动态地增长或收缩。

C# 集合的类型

有 3 种处理集合的方式。以下是这三个命名空间

  • System.Collections.Generic
  • System.Collections 类(现已弃用)
  • System.Collections.Concurrent

1) System.Collections.Generic 类

System.Collections.Generic 命名空间包含以下类

  • 列表
  • Stack
  • Queue
  • LinkedList
  • HashSet
  • SortedSet
  • 字典
  • SortedDictionary
  • SortedList

2) System.Collections 类

这些类是旧的。现在建议使用 System.Collections.Generic 类。System.Collections 命名空间包含以下类

  • ArrayList
  • Stack
  • Queue
  • Hashtable

3) System.Collections.Concurrent 类

System.Collections.Concurrent 命名空间提供用于线程安全操作的类。现在,多个线程访问集合项不会造成问题。

System.Collections.Concurrent 命名空间包含以下类

  • BlockingCollection
  • ConcurrentBag
  • ConcurrentStack
  • ConcurrentQueue
  • ConcurrentDictionary
  • Partitioner
  • Partitioner
  • OrderablePartitioner
下一主题C# List