为什么列表< T>实现IList<吨>中的ICollection< T>和IEnumerable< T&GT ;? [英] Why does List<T> implement IList<T>, ICollection<T> and IEnumerable<T>?

查看:196
本文介绍了为什么列表< T>实现IList<吨>中的ICollection< T>和IEnumerable< T&GT ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果你去的定义列表< T> 你会看到以下内容:

If you go to definition of List<T> you would see the following:

public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>

的IList&LT; T&GT; 从已经继承了的ICollection&LT; T&GT; 的IEnumerable&LT; T&GT;

那岂不是有,如果已经足够名单&LT; T&GT; 只执行的IList&LT; T&GT;

Wouldn't it have been sufficient if List<T> only implemented IList<T>?

推荐答案

是的,这使得在这种情况下没有什么区别。在某些情况下,它的可以的有所作为,如果你使用的已实现接口的基类,但你要自己明确地重新实现它 - 但在这种情况下,没有基类(比其他隐对象),它会表现得完全相同的方式。

Yes, it makes no difference in this case. In some cases it can make a difference, if you're using a base class which already implements an interface but you wish to reimplement it yourself explicitly - but in this case there's no base class (other than the implicit object) and it would have behaved exactly the same way.

出乎我的回忆,我不相信有一个在类中重元psented $ P $的方式的差异$ C $是否Ç显式声明所有接口或没有。这里有一个例子:

Contrary to my recollections, I don't believe there's a difference in the way the class is represented in metadata whether the code explicitly declares all the interfaces or not. Here's an example:

interface IFoo {}
interface IBar : IFoo {}

class FooBar1 : IBar {}
class FooBar2 : IBar, IFoo {}

这两个ILDASM和反射显示 FooBar1 FooBar2 ...它显示了两者实现相同的信息伊巴尔的IFoo

Both ildasm and Reflector show the same information for FooBar1 and FooBar2... it shows both of them implementing IBar and IFoo.

在换句话说,我们无法判断对原始出处code列表与LT; T&GT; 其实指定所有接口或没有。也许是这样,也许它没有 - 但没关系不管怎样

In other words, we can't tell whether the original source code for List<T> actually specifies all the interfaces or not. Maybe it does, maybe it doesn't - but it doesn't matter either way.

编辑:为了完整起见,我还检查你在哪里扩展两个接口与另一个接口的情况下。我不能找到在这种情况下,元数据的差异,无论是。我敢肯定,我记得一些情况中,很明显,但现在我找不到它。

For completeness, I also checked the cases where you're extending two interfaces with another interface. I can't find a difference in the metadata in that case, either. I'm sure I remember some situation in which it was evident, but I can't find it now.

这篇关于为什么列表&LT; T&GT;实现IList&LT;吨&gt;中的ICollection&LT; T&GT;和IEnumerable&LT; T&GT ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆