我将编写一个库来遍历对象图(类似某种序列化)。
在遍历过程中,您需要判断对象是否为集合,因此我想到了
但是,几乎所有的容器集合都已经实现了
我已经检查了
在遍历过程中,您需要判断对象是否为集合,因此我想到了
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
这是一个BUG吗?还是有其他原因导致的?
List<T>
实现ICollection
显然有助于替换旧的ArrayList
用法(理论上最好只是让编译器出现错误,指向需要升级为ICollection<T>
的位置)。在引入3.5之前,HashSet<T>
没有非泛型版本,因此支持ICollection
与其他大多数集合类型不同。 - Jon HannaSortedSet<>
是一种更新的类型,它确实实现了非泛型的ICollection
吗? - Jeppe Stig NielsenList<T>
也不应该这样,但是对于HashSet<T>
和SortedSet<T>
,你会有更强的理由。这是设计决策,因此更多的是关于你能够争论出什么作为某人所争论的线索,而不是自然界中不可变的法则。 - Jon Hanna