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

后端 未结 4 889

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:58

    You need to use explicit interface implementation for this process.

    If the two interface members do not perform the same function, however, this can lead to an incorrect implementation of one or both of the interfaces. It is possible to implement an interface member explicitly—creating a class member that is only called through the interface, and is specific to that interface. This is accomplished by naming the class member with the name of the interface and a period.

    interface IA
    {
       void Display();
    }
    interface IB
    {
       void Display();
    }
    
        public class Program:IA,IB
        {
    
            void IA.Display()
            {
                Console.WriteLine("I am from A");
            }
    
            void IB.Display()
            {
                Console.WriteLine("I am from B");
            }
    
            public static void Main(string[] args)
            {
               IA p1 = new Program();
               p1.Display();
               IB p2 = new Program();
               p2.Display();
            }
        }
    

    Output will be:

    I am from A
    I am from B
    

    Here is a DEMO.

    0 讨论(0)
  • 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();
        }
    
    0 讨论(0)
  • 2021-01-19 04:00

    In order to call an explicit interface method you must hold a reference to that interface type or cast it:

    IB ib = new Model();
    ib.Display();
    
    IA ia = (IA)ib;
    ia.Display();
    
    0 讨论(0)
  • 2021-01-19 04:02

    The method that will be called depends on the type that calls it. For example:

    Note, for sake of clarity, I create two items. In practice though, you don't want to do this, you should just cast the object between types.

    static void Main(string[] args)
    {
        // easy to understand version:
        IA a = new Model();
        IB b = new Model();
    
        a.Display();
        b.Display();
    
        // better practice version:
        Model model = new Model();
    
        (IA)model.Display();
        (IB)model.Display();
    }
    
    interface IA
    {
        void Display();
    }
    
    interface IB
    {
        void Display();
    }
    
    class Model : IA, IB
    {
        void IA.Display()
        {
            Debug.WriteLine("I am from A");
        }
    
        void IB.Display()
        {
            Debug.WriteLine("I am from B");
        }            
    }
    

    Outputs:

    I am from A
    I am from B
    
    0 讨论(0)
提交回复
热议问题