如果这些接口之一从其余接口继承,为什么 C# 中的集合类(如 ArrayList)从多个接口继承? [英] Why collections classes in C# (like ArrayList) inherit from multiple interfaces if one of these interfaces inherits from the remaining?

查看:34
本文介绍了如果这些接口之一从其余接口继承,为什么 C# 中的集合类(如 ArrayList)从多个接口继承?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在ArrayList关键字上按f12进入vs2008生成的元数据时,发现生成的类声明如下

When I press f12 on the ArrayList keyword to go to metadata generated from vs2008, I found that the generated class declaration as follows

public class ArrayList : IList, ICollection, IEnumerable, ICloneable

我知道IList已经继承了ICollection和IEnumerable,那为什么ArrayList还要冗余继承这些接口呢?

I know that the IList already inherits from ICollection and IEnumerable, so why does ArrayList redundantly inherit from these interfaces?

推荐答案

好的,我已经做了一些研究.如果您创建以下层次结构:

OK, I've done some research. If you create the following hierarchy:

  public interface One
    {
        void DoIt();
    }

    public interface Two : One
    {
        void DoItMore();
    }

    public class Magic : Two
    { 
        public void DoItMore()
        {
            throw new NotImplementedException();
        }

        public void DoIt()
        {
            throw new NotImplementedException();
        }
    }

并编译它,然后在不同的解决方案中引用该DLL,键入Magic并按F12,您将得到以下内容:

And compile it, then reference the DLL in a different solution, type Magic and Press F12, you will get the following:

 public class Magic : Two, One
    {
        public Magic();

        public void DoIt();
        public void DoItMore();
    }

您会看到接口层次结构被扁平化,还是编译器正在添加接口?如果您使用反射器,您也会得到相同的结果.

You will see that the interface hierarchy is flattened, or the compiler is adding the interfaces in? If you use reflector you get the same results too.

更新:如果你在 ILDASM 中打开 DLL,你会看到它说:

Update: If you open the DLL in ILDASM, you will see it saying:

实现...两个

实现...一个.

这篇关于如果这些接口之一从其余接口继承,为什么 C# 中的集合类(如 ArrayList)从多个接口继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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