【发布时间】:2015-09-25 04:52:03
【问题描述】:
我将编写一个库来遍历对象图(例如某种序列化)。
您需要判断一个对象是否是遍历中的一个集合,所以ICollection 出现在我的脑海中。 (string也实现了IEnumerable)
但是真的很奇怪,Collections 中几乎所有的容器都实现了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
这是一个错误吗?还是有什么原因?
【问题讨论】:
-
我相信this link 与您的问题相关。
-
.NET 集合层次结构真的很乱。
-
如果您将 1.1 代码升级到 2.0,那么
List<T>实现ICollection可以说可以帮助您替换ArrayList的旧用法(可以说让编译器错误指向哪里会更好)您需要升级到ICollection<T>)。HashSet<T>在 3.5 引入之前没有非通用版本,因此支持ICollection和其他大多数人的原因之一不存在。 -
@JonHanna 但是
SortedSet<>是一个更新的类型,它确实实现了非泛型ICollection?
标签: .net collections interface hashset