[英]Why does List<T> implement IList<T>, ICollection<T> and IEnumerable<T>?
If you go to definition of List<T>
you would see the following: 如果你去定义
List<T>
你会看到以下内容:
public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>
IList<T>
already inherits from both ICollection<T>
and IEnumerable<T>
. IList<T>
已经从ICollection<T>
和IEnumerable<T>
继承。
Wouldn't it have been sufficient if List<T>
only implemented IList<T>
? 如果
List<T>
只实现了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. 在某些情况下,如果您使用的是已经实现了接口的基类,但是您希望自己重新实现它,那么它可能会有所不同 - 但在这种情况下,没有基类(除了隐式
object
)并且它会有表现完全一样。
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 {}
Both ildasm and Reflector show the same information for FooBar1
and FooBar2
... it shows both of them implementing IBar
and IFoo
. ildasm和Reflector都显示了与
FooBar1
和FooBar2
相同的信息......它显示了它们都实现了IBar
和IFoo
。
In other words, we can't tell whether the original source code for List<T>
actually specifies all the interfaces or not. 换句话说,我们无法判断
List<T>
的原始源代码是否实际指定了所有接口。 Maybe it does, maybe it doesn't - but it doesn't matter either way. 也许它确实如此,也许它没有 - 但无论如何都无所谓。
EDIT: 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.
我确信我记得有些情况很明显,但我现在找不到它。
Yes it would. 是的,它会的。
IList<T>
itself implements the other two. IList<T>
本身实现了另外两个。
The object browser shows you all the interfaces the class implements, whether directly ( IList<T>
) or indirectly ( ICollection<T>
, IEnumerable<T>
, through IList<T>
). 对象浏览器向您显示该类实现的所有接口,无论是直接(
IList<T>
)还是间接( ICollection<T>
, IEnumerable<T>
, 通过 IList<T>
)。
That's not the way it was actually coded behind the scenes. 这不是它在幕后实际编码的方式。 That's just what tools like Reflector show you when it turns the IL back into C#.
这就是Reflector等工具在将IL转回C#时向您展示的内容。
If you looked at the source code: 如果查看源代码:
https://referencesource.microsoft.com/#mscorlib/system/collections/generic/list.cs https://referencesource.microsoft.com/#mscorlib/system/collections/generic/list.cs
You can observe that the signature is: 您可以观察到签名是:
public class List<T> : IList<T>, System.Collections.IList, IReadOnlyList<T>
This can only mean that the metadata browser de-normalizes the inheritance hierarchy for us. 这只能意味着元数据浏览器会为我们对继承层次结构进行反规范化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.