When you implement two interfaces with the same method, how do you know which one is called?

后端 未结 8 1537
再見小時候
再見小時候 2021-01-13 05:18

If you have TheMethod() in interfaces I1 and I2, and the following class

class TheClass : I1, I2
{
    void TheMethod()
}

If s

8条回答
  •  灰色年华
    2021-01-13 05:55

    public interface IA
    {
       void Sum();
    }
    
    public interface IB
    {
        void Sum();
    }
    
    public class SumC : IA, IB
    {
       void IA.Sum()
        {
            Console.WriteLine("IA");
        }
    
       void IB.Sum()
       {
           Console.WriteLine("IB");
       }
      public void Sum()
       {
           Console.WriteLine("Default");
       }
    }
    
    public class MainClass
    {
        static void Main()
        {
            IA objIA = new SumC();
            IB objIB = new SumC();
            SumC objC = new SumC();
    
            objIA.Sum();
            objIB.Sum();
            objC.Sum();
    
            Console.ReadLine();
        }
    }
    

提交回复
热议问题