0

In what cases make sense to have implicit end explicit implementation of same method from interface?

I know difference, but I don't know why sometimes are both used?

    interface I1
    {
        void A();
    }

    class B : I1
    {
        public void A()
        {
            Console.WriteLine("Implicit ");
        }
        void void I1.A()
        {
            Console.WriteLine("Explicit");
        }
    }
Raskolnikov
  • 3,791
  • 9
  • 43
  • 88
  • Possible duplicate: http://stackoverflow.com/questions/598714/implicit-vs-explicit-interface-implementation – Kzryzstof Dec 17 '15 at 13:08
  • 2
    Can you explain how that is a duplicate? I find it hard to read an answer to this question there. – Patrick Hofman Dec 17 '15 at 13:11
  • The anwser provided by Andrew Barrett is pretty clear in itself: "In terms of when you would use one over the other,....", along with the link to MSDN blog :) – Kzryzstof Dec 17 '15 at 13:15

1 Answers1

1

You can do that for example to make the method protected and accessible through the interface.

In that way, callers can only access it though the interface declaration, or if it derives from the class. Explicit interface members can't be accessed from the class itself or from derived classes.

class B : I1
{
    protected void A()
    {
        Console.WriteLine("Implicit ");
    }
    void void I1.A()
    {
        Console.WriteLine("Explicit");
    }
}

I1 i = new B();
i.A(); // works

B b = new B();
b.A(); // does not work
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • In this case should work both, because there is implicit and explicit implementation of A() method. Just result is different. – Raskolnikov Dec 17 '15 at 13:23
  • No, you can't access protected members from outside. – Patrick Hofman Dec 17 '15 at 13:23
  • O yess sory , I see that, but what is than point of protected method? I can use only explicit? – Raskolnikov Dec 17 '15 at 13:27
  • You can, but you asked why you would want to implement it like this. Explicit interface members can't be accessed from the class itself or from derived classes. `protected` can. – Patrick Hofman Dec 17 '15 at 13:28
  • " Explicit interface members can't be accessed from the class itself" that is answer to my question. Add that sentence please to your answer. – Raskolnikov Dec 17 '15 at 13:35