Why C# doesn't allow inheritance of return type when implementing an Interface

后端 未结 6 697
一生所求
一生所求 2020-11-28 14:24

Is there any rational reason why the code below is not legal in C#?

class X: IA, IB
{
    public X test() // Compliation Error, saying that X is not IB
    {         


        
6条回答
  •  鱼传尺愫
    2020-11-28 14:48

    You could use explicit interface implementation to avoid the problem.

    class  X : IA, IB
    {
      public X test()
      {
        return this;
      }
    
      IB IA.test()
      {
        return this;
      }
    }
    
    interface IA
    {
      IB test();
    }
    
    interface IB
    {
    }
    

提交回复
热议问题