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

16,688

Solution 1

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();
    }

Solution 2

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

Solution 3

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.

Solution 4

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();
Share:
16,688
Jasmine
Author by

Jasmine

Updated on July 29, 2022

Comments

  • Jasmine
    Jasmine almost 2 years

    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 code where I declare the interface methods "public" in which case it works. I dont know how to call it when I explicitly declare :( I am learning C#....

    interface IA
        {
            void Display();
        }
        interface IB
        {
            void Display();
        }
        class Model : IA, IB
        {
            void IA.Display()
            {
                Console.WriteLine("I am from A");
            }
            void IB.Display()
            {
                Console.WriteLine("I am from B");
            }
            //public void Display()
            //{
            //    Console.WriteLine("I am from the class method");
            //}
    
            static void Main()
            {
                Model m = new Model();
                //m.Display();
                Console.ReadLine();
            }
        }
    
  • BlackBear
    BlackBear about 11 years
    Perhaps (m as IB).Display() would be more readable, +1 though
  • Jasmine
    Jasmine about 11 years
    Thank you so much Adam, thats simple :) :) Cheers
  • Jasmine
    Jasmine about 11 years
    Hi Reed, thank you so much, it works, but honestly I don't understand what IA asIA = m mean. Its an assignment ? I mean, I dont understand how it works, could you please explain me in some basic terms ? I would appreciate that :) :)
  • Daniel Brückner
    Daniel Brückner about 11 years
    (m as IB).Display() is not better because it carries the wrong semantics. If you know x of type T always use (T)x. x as T is for the case that x might be of type T and you check afterwards by testing if you got null or not.
  • Jasmine
    Jasmine about 11 years
    But Adam, is it costly to do this way rather than casting method as shown by some friends above ? As because I see we use NEW two times... ??
  • Reed Copsey
    Reed Copsey about 11 years
    @Divine You're assigning the instance to a variable of type "IA". Since the class implements IA, the assignment is legal. When you call asIA.Display(), it's calling via the interface, so you get the explicit implementation.
  • Jasmine
    Jasmine about 11 years
    Thats perfectly working thank you :) :) May I know whats the difference between IA ia = new Model() and IA ia = (IA)ib ??? Is it called casting one type to other ?
  • Jasmine
    Jasmine about 11 years
    @ReedCopsey: Thank you, I understand what you said. Also I have another basic doubt, here someone has done IA a = new Model(); IB b = new Model(); Code wise that appeared simple to me, but is there any difference ? Which operation is costly ? As I feel here we use new two times, its costly ?
  • Bronumski
    Bronumski about 11 years
    @Divine It means that ib, because Model implements IA can be CAST to an IA just as it could be cast back to Model. However an object that implements just IB could not be cast to IA. All the answers on here are very similar.
  • Reed Copsey
    Reed Copsey about 11 years
    @Divine Neither is more costly - you'd use the one that's appropriate in your scenario. Typically, if you're implementing an interface, it's because you're going to make routines that work against that interface, not against the class itself. At some point, you'll end up "assigning" the instances to variables defined by the interface. This may be a method call parameter, explicitly, etc - it really doesn't matter. The assignment itself is very cheap, so it's not a matter of "cost" but of intention.
  • Jasmine
    Jasmine about 11 years
    @ReedCopsey: Thank you so much for your time and help in explaining me :) I appreciate it greatly :)
  • Jasmine
    Jasmine about 11 years
    @DanielBrückner: Thank you Daniel :) Cheers
  • Jasmine
    Jasmine about 11 years
    thank you so much for the help and making me understand :) I really appreciate it, cheers :)
  • Jasmine
    Jasmine about 11 years
    Thank you so much Soner for the help :) Appreciate it, cheers :)
  • Adam K Dean
    Adam K Dean about 11 years
    I created two items for clarity only, I wouldn't recommend doing that in practice though, I would suggest you follow Reed Copsey's answer. Glad it could help you understand though!
  • Jasmine
    Jasmine about 11 years
    Thank you so much Adam, yes as you said, when I first saw it as a novice, it was very clear and easy code to me :) Well will remember the best practises as suggested by Reed...Thanks again Adam... :)
  • Adam K Dean
    Adam K Dean about 11 years
    I've added a small change to my code, might be a little bit more readable than the other answers posted on here.
  • Jasmine
    Jasmine about 11 years
    Awesome, both sounds very understandable, thanks again :) Cheers