为什么IList
和ICollection
的通用对应方不具有相同的一组方法和属性有什么特别的原因吗?他们似乎把他们移动了。为什么IList和ICollection的通用副本不具有相同的方法集?
Ex。 IList<T>
有
int IndexOf(T item);
void Insert(int index, T item);
void RemoveAt(int index);
T this[int index] { get; set; }
但IList
有
int Add(object value);
void Clear();
bool Contains(object value);
int IndexOf(object value);
void Insert(int index, object value);
void Remove(object value);
void RemoveAt(int index);
bool IsFixedSize { get; }
bool IsReadOnly { get; }
object this[int index] { get; set; }
我完全同意你的看法,ICollection应该有所有这些方法。奇怪的是,只有通用版本才有。 – Magnus 2011-05-12 19:25:22
@Magnus:'ICollection'包括应该出现在非泛型'ICollection'和'IList'之间的类型的功能。相反,在“ICollection ”和“IEnumerable ”之间,应该有一种类似于非通用ICollection 的类型,但不能添加项目。当泛型和非泛型事物如此不同时,使用相同的名称“ICollection”会令人困惑;减轻混淆的最好方法是忽略匹配的名称。 –
supercat
2014-02-11 07:25:48