Mr.*_*Ree 6 .net collections interface hashset
我将编写一个库来遍历一个对象图(就像某种序列化一样).
您将需要判断一个对象是否是遍历中的集合,因此ICollection我的想法就出现了.(string也已实施IEnumerable)
但是,ICollection除了HashSet仅实施之外,几乎集合中的所有容器都已实现,这真的很奇怪ICollection<T>......
我检查了System.Collections命名空间中的几乎所有常见容器:
ArrayList : IList, ICollection, IEnumerable, ICloneable
BitArray : ICollection, IEnumerable, ICloneable
Hashtable : IDictionary, ICollection, IEnumerable, ISerializable, IDeserializationCallback, ICloneable
Queue : ICollection, IEnumerable, ICloneable
SortedList : IDictionary, ICollection, IEnumerable, ICloneable
Stack : ICollection, IEnumerable, ICloneable
Dictionary<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IDictionary, ICollection, IReadOnlyDictionary<TKey, TValue>, IReadOnlyCollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IEnumerable, ISerializable, IDeserializationCallback
HashSet<T> : ISerializable, IDeserializationCallback, ISet<T>, ICollection<T>, IEnumerable<T>, IEnumerable
LinkedList<T> : ICollection<T>, IEnumerable<T>, ICollection, IEnumerable, ISerializable, IDeserializationCallback
List<T> : IList<T>, ICollection<T>, IList, ICollection, IReadOnlyList<T>, IReadOnlyCollection<T>, IEnumerable<T>, IEnumerable
Queue<T> : IEnumerable<T>, ICollection, IEnumerable
SortedDictionary<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IDictionary, ICollection, IEnumerable
SortedList<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IDictionary, ICollection, IEnumerable
SortedSet<T> : ISet<T>, ICollection<T>, IEnumerable<T>, ICollection, IEnumerable, ISerializable, IDeserializationCallback
Stack<T> : IEnumerable<T>, ICollection, IEnumerable
Run Code Online (Sandbox Code Playgroud)
这是一个错误吗?或者背后有一些原因?
ICollection现在,它的用处远不如 .NET 1.1 那样有用,当时没有ICollection<T>提供更高的类型安全性。几乎没有什么是ICollection人们不能做的ICollection<T>,通常具有更高的效率和/或类型安全性,特别是如果人们为可能想要对不同元素类型的集合执行某些操作的情况编写泛型方法。
但这引出了一个问题:为什么像这样的人List<T> 确实实施了ICollection。但是,当List<T>.NET 2.0 引入时,所有遗留代码都使用ICollectionand ArrayList,而不是ICollection<T>and List<T>。升级代码以使用List<T>而不是ArrayList可能会很痛苦,特别是如果这意味着必须立即更改ICollection它的所有用途ICollection<T>,或者更糟糕的是,由于一个方法被List<T>其他方法击中,所以要加倍。非泛型集合,因此每个集合都需要该方法的版本。实施ICollection通过允许人们更加零散地利用通用集合来简化升级路径。
泛型问世时HashSet<T>已经使用了三年,并且框架之前没有提供非泛型哈希集类型,因此升级的痛苦较少,因此支持的动机也较少ICollection。
| 归档时间: |
|
| 查看次数: |
1321 次 |
| 最近记录: |