为什么C#中的集合类(如ArrayList)从多个接口继承,如果其中一个接口继承其余接口?

Ahm*_*aid 31 c# inheritance interface arraylist

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

public class ArrayList : IList, ICollection, IEnumerable, ICloneable
Run Code Online (Sandbox Code Playgroud)

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

Ric*_*dOD 14

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

  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();
        }
    }
Run Code Online (Sandbox Code Playgroud)

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

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

        public void DoIt();
        public void DoItMore();
    }
Run Code Online (Sandbox Code Playgroud)

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

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

实施......两个

实施......一个.