How to call a particular explicitly declared interface method in C#

后端 未结 4 890

I have a doubt on how to call the method of particular interface (Say IA or IB or In...) in the following code. Please help me on how to call. I have commented the lines of

4条回答
  •  粉色の甜心
    2021-01-19 03:59

    To call an explicit interface method, you need to use a variable of the proper type, or directly cast to that interface:

        static void Main()
        {
            Model m = new Model();
    
            // Set to IA
            IA asIA = m;
            asIA.Display();
    
            // Or use cast inline
            ((IB)m).Display();
    
            Console.ReadLine();
        }
    

提交回复
热议问题