Why collections classes in C# (like ArrayList) inherit from multiple interfaces if one of these interfaces inherits from the remaining?

前端 未结 6 1049
南笙
南笙 2020-12-08 07:28

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 Array         


        
6条回答
  •  北海茫月
    2020-12-08 07:30

    My guess would be that the CLR does not support an interface that inherits from another interface.

    C# however does support this construct but has to 'flatten' the inheritance tree to be CLR compliant.

    [Edit]

    After taking advise from below quickly setup a VB.Net project:

    Public Interface IOne
        Sub DoIt()
    End Interface
    
    Public Interface ITwo
        Inherits IOne
        Sub DoIt2()
    End Interface
    
    Public Class Class1
        Implements ITwo
    
        Public Sub DoIt() Implements IOne.DoIt
            Throw New NotImplementedException()
        End Sub
    
        Public Sub DoIt2() Implements ITwo.DoIt2
            Throw New NotImplementedException()
        End Sub
    End Class
    

    Compiling results in the following (C#):

    public class Class1 : ITwo
    {
        public Class1();
        public void DoIt();
        public void DoIt2();
    }
    

    This show that VB.Net does NOT flatten the interface hierarchy as opposed to C#. I have no clue as to why this would be.

提交回复
热议问题