C# .net框架列表<;T>;遗产
MS源代码 在.netC# .net框架列表<;T>;遗产,c#,.net,multiple-inheritance,C#,.net,Multiple Inheritance,MS源代码 在.net列表中类继承两个接口 (IList和System.Collections.IList) 并实现了两种方法的加法 public void Add(T item) { if (_size == _items.Length) EnsureCapacity(_size + 1); _items[_size++] = item; _version++; } int System.Collections.IList.Add(Ob
列表中
类继承两个接口
(IList
和System.Collections.IList
)
并实现了两种方法的加法
public void Add(T item) {
if (_size == _items.Length) EnsureCapacity(_size + 1);
_items[_size++] = item;
_version++;
}
int System.Collections.IList.Add(Object item)
{
ThrowHelper.IfNullAndNullsAreIllegalThenThrow<T>(item, ExceptionArgument.item);
try {
Add((T) item);
}
catch (InvalidCastException) {
ThrowHelper.ThrowWrongValueTypeArgumentException(item, typeof(T));
}
return Count - 1;
}
公共作废添加(T项){
如果(_size==_items.Length)确保可恢复性(_size+1);
_项目[_size++]=项目;
_版本++;
}
int System.Collections.IList.Add(对象项)
{
ThrowHelper.ifnullandnullsareilegalthenthrow(项,argument.item除外);
试试{
增加((T)项);
}
捕获(无效卡斯特例外){
ThrowWhelper.ThrowWingValueTypeArgumentException(项目,类型(T));
}
返回计数-1;
}
第一个方法是public,第二个方法是private
IList没有add方法,但System.Collections.IList有
inherit from System.Collections.IList的概念是什么?首先,正如其他人已经指出的,类实现接口而不是继承它们
int System.Collections.IList.Add(Object item)
在这里,类显式地实现接口的Add()方法。与隐式接口实现相反,这些方法只能使用接口引用而不是类引用来访问。请参阅此处,以了解隐式接口实现和显式接口实现之间的详细区别。为了清楚起见,类实现接口并从基类继承。类不继承接口。(接口可以从基本接口继承)。
List
不从IList
和IList
继承,而是实现它们,因为它们是接口,而不是类。您可以实现任意多个接口—这不是“多重继承”。第二个接口不是私有接口,它只是System.Collections.IList.Add方法的显式实现“继承自System.Collections.IList
的概念是什么?”-这样一来,List
就可以在任何需要System.Collections.IList
类型的地方使用-这在处理非泛型代码时尤为重要。就这么简单。