A question about interface inheritance in .NET

前端 未结 4 946
慢半拍i
慢半拍i 2021-01-06 18:11

Let\'s take the interface IQueryable for example:

public interface IQueryable : IQueryable, IEnumerable;

Sin

4条回答
  •  日久生厌
    2021-01-06 18:29

    Its because Interface inheritance is a pure language sugar thing. When compiling to IL, the interface explicitly implements all the interfaces in the chain so looking at the signature for IQueryable will end up looking like

    .class public interface abstract auto ansi IQueryable<+ T>
        implements [mscorlib]System.Collections.Generic.IEnumerable`1, System.Linq.IQueryable, [mscorlib]System.Collections.IEnumerable
    {
    }
    

    And since most documentation is auto generated from metadata, and not the original sourcecode, it will end up showing the signature as its implementing all the interfaces directly, and not via inheritance. Which is basically how it is. The only reason YOU can take advantage of inheritance is because the compiler will end up creating the right signature for you, wiring up all the interfaces explicitly.

提交回复
热议问题