Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么IList和ICollection的泛型对应项没有相同的方法集?_C#_Generics_Ilist_Icollection - Fatal编程技术网

C# 为什么IList和ICollection的泛型对应项没有相同的方法集?

C# 为什么IList和ICollection的泛型对应项没有相同的方法集?,c#,generics,ilist,icollection,C#,Generics,Ilist,Icollection,为什么IList和ICollection的泛型对应项没有相同的方法和属性集,这有什么特别的原因吗?他们似乎把他们弄来弄去了 前。 IList 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

为什么
IList
ICollection
的泛型对应项没有相同的方法和属性集,这有什么特别的原因吗?他们似乎把他们弄来弄去了

前。
IList

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; }

IList
实现了
ICollection
,因此至少在
IList
的上下文中,哪个接口有什么并不重要。至于原因,谁知道这个疯狂的.NET团队在想什么呢。然而,“集合”需要添加、删除、检查其内容等方法,只要这些方法不在索引的“列表”上下文中,这似乎是合乎逻辑的。一旦从概念上开始讨论列表,就需要添加索引,这需要索引器本身,以及添加、删除和检查元素的索引敏感方法。

IList
实现了
ICollection
,因此至少在
IList
的上下文中,哪个接口有什么并不重要。至于原因,谁知道这个疯狂的.NET团队在想什么呢。然而,“集合”需要添加、删除、检查其内容等方法,只要这些方法不在索引的“列表”上下文中,这似乎是合乎逻辑的。一旦开始从概念上讨论列表,就需要添加索引(这需要索引器本身),以及添加、删除和检查元素的索引敏感方法。

我认为主要的设计决策归结于一个事实,即.Net中的数组实际上都隐式实现了IList。
IList
中的许多方法,例如
Remove
Clear
都不适用于数组,因此它们被移动到了不同的接口。

我认为主要的设计决策归结于.Net中的数组实际上都隐式地实现了
IList
IList
中的许多方法,如
Remove
Clear
不适用于数组,因此它们被移动到不同的接口。

数组也实现了非通用的
IList
中还有一个Add方法,它是“已实现的”数组也实现了非泛型的
IList
IList
中还有一个Add方法,它是由数组“实现”的。我完全同意你的观点,ICollection应该有所有这些方法。奇怪的是,只有泛型版本才有。@Magnus:ICollection
ICollection
包含的功能应该出现在非泛型ICollection和IList之间的类型中。相反,在
ICollection
IEnumerable
之间,应该有一个类似于非泛型
ICollection
的类型,但不能添加项。当泛型和非泛型事物如此不同时,使用相同的名称
ICollection
,是令人困惑的;缓解这种混乱的最好方法是忽略匹配的名称。我完全同意您的观点,ICollection应该包含所有这些方法。奇怪的是,只有泛型版本才有。@Magnus:ICollection
ICollection
包含的功能应该出现在非泛型ICollection和IList之间的类型中。相反,在
ICollection
IEnumerable
之间,应该有一个类似于非泛型
ICollection
的类型,但不能添加项。当泛型和非泛型事物如此不同时,使用相同的名称
ICollection
,是令人困惑的;缓解这种混乱的最好方法是忽略匹配的名称。