在 C# 中,集合表示对象组。借助集合,我们可以对对象进行各种操作,例如
存储对象
更新对象
删除对象
检索对象
搜索对象,以及
排序对象
在排序中,所有的数据结构工作都可以由 C# 集合来完成。
我们可以将对象存储在数组或集合中。集合比数组更有优势。数组有大小限制,但存储在集合中的对象可以动态增长或收缩。
有 3 种方法可以处理集合。下面给出了三个命名空间:
System.Collections.Generic classes
System.Collections classes (Now deprecated)
System.Collections.Concurrent classes
System.Collections.Generic 命名空间具有以下类:
List
Stack
Queue
LinkedList
HashSet
SortedSet
Dictionary
SortedDictionary
SortedList
这些类是遗留的。现在建议使用 System.Collections.Generic 类。System.Collections 命名空间有以下类:
ArrayList
Stack
Queue
Hashtable
System.Collections.Concurrent 命名空间为线程安全操作提供类。现在多个线程不会对访问集合项产生问题。
System.Collections.Concurrent 命名空间有以下类:
BlockingCollection
ConcurrentBag
ConcurrentStack
ConcurrentQueue
ConcurrentDictionary
Partitioner
Partitioner
OrderablePartitioner