简体   繁体   English

为什么列表 <T> 实现IList <T> ,ICollection <T> 和IEnumerable <T> ?

[英]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都显示了与FooBar1FooBar2相同的信息......它显示了它们都实现了IBarIFoo

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.

相关问题 为什么ICollection <T>实现IEnumerable <T>和IEnumerable - Why does ICollection<T> implement both IEnumerable<T> and IEnumerable 为什么使用 ICollection<t> 在 IList<t> ?</t></t> - Why use ICollection<T> over IList<T>? 为什么IEnumerable <T> .ToList <T>()返回List <T>而不是IList <T>? - Why does IEnumerable<T>.ToList<T>() return List<T> instead of IList<T>? 如何实现ICollection <T> 在一个IEnumerable上 <T> - How to implement ICollection<T> on an IEnumerable<T> 为什么不 IList<t> 仅从 ICollection 继承<t> ?</t></t> - Why doesn't IList<T> only inherit from ICollection<T>? 与ICollection和IEnumerable一起使用<T> - work with ICollection and IEnumerable<T> 清单 <T> 比“更好” <T> ? 以及为什么要实现冗余接口? - List<T> is “better” then ICollection<T>? And why implement redundant interfaces? 为什么选择列表<T>继承 IEnumerable<T> 和 IEnumerable 再次 - Why IList<T> inherits IEnumerable<T> and IEnumerable again 重载函数以接受IEnumerable是否常见(或鼓励)实践 <T> ,ICollection <T> ,IList <T> 等等? - Is it common (or encouraged) practice to overload a function to accept IEnumerable<T>, ICollection<T>, IList<T>, etc.? IList的TypeDependencyAttribute(“System.SZArrayHelper”)的用途 <T> ,IEnumerable <T> 和ICollection <T> ? - Purpose of TypeDependencyAttribute(“System.SZArrayHelper”) for IList<T>, IEnumerable<T> and ICollection<T>?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM