为什么列表< 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.