范型集合
类型 | 增加项 | 删除项 | 查找项 | 长度限制 | 是否可拷贝 |
Array | O(n)* | O(n)* | O(n) | 是 | 是 |
List<T> | O(n)* | O(n) | O(n) | 否 | 是 |
LinkedList<T> | O(1) | O(1) | O(n) | 否 | 是 |
SortedList<TKey, TValue> | O(n) | O(n) | O(log n) | 否 | Keys:否 Values:是 |
Stack<T> | O(1) | O(1) | O(n) | 否 | 是 |
Queue<T> | O(1) | O(1) | O(n) | 否 | 是 |
HashSet<T> | O(1) | O(1) | O(1) | 否 | 否 |
SortedSet<T> | O(1) | O(1) | O(1) | 否 | 否 |
Dictionary<TKey, TValue> | O(1) | O(1) | O(1) | 否 | Keys:否 Values:是 |
SortedDictionary<TKey, TValue> | O(log n) | O(log n) | O(log n) | 否 | Keys:否 Values:是 |
非泛型集合
集合类型 | 描述 |
ArrayList | 与List<object>相似 |
BitArray | 布尔类型的位值数组,可执行布尔逻辑运算。 |
BitVector32 | 与BitArray相似,限制在32位。 |
ListDictionary | 使用单链接列表实现IDictionary,与list使用相似。专为非常小的集合设计(元素数量是10或小于10),复杂度是O(n)。 |
HybridDictionary | 在集合较小时,使用ListDictionary来实现IDictionary,然后当集合变大时,切换到Hashtable。 |
StringCollection | 与List<string>相同。 |
StringDictionary | 与Dictionary<string, string>相同。 |
自定义的集合接口
接口 | 描述 | 方法 |
ICollection<T> | 适用大部分集合类型的基本方法。继承了IEnumerable<T>接口。 | Add, Clear, Contains, CopyTo, Remove |
IEnumerable<T> | 指定了集合中的元素可以使用foreach遍历 | GetEnumerator |
IDictionary<TKey, TValue> | 可以通过键值对了访问集合中的元素,继承自ICollection<KeyValuePair<TKey, TValue>>接口 | ContainsKey, TryGetValue |
IList<T> | 指定了集合可以通过索引访问,继承自ICollection<T>接口 | IndexOf, Insert, RemoveAt |