7

Possible Duplicates:
C#: Interfaces - Implicit and Explicit implementation
implicit vs explicit interface implementation

Hello

Can anyone explain me what the difference is between an implicit and explicit interface?

Thanks!

Community
  • 1
  • 1
Killerwes
  • 292
  • 1
  • 4
  • 14

4 Answers4

10

When you implement an interface explicitlty, the methods on that interface will only be visible if the object is referenced as the interface:

public interface IFoo
{
   void Bar();
}

public interface IWhatever
{
   void Method();
}

public class MyClass : IFoo, IWhatever
{
   public void IFoo.Bar() //Explicit implementation
   {

   }

   public void Method() //standard implementation
   {

   }
}

If somewhere in your code you have a reference to this object:

MyClass mc = new MyClass();
mc.Bar(); //will not compile

IFoo mc = new MyClass();
mc.Bar(); //will compile

For the standard implementation, it doesn't matter how you reference the object:

MyClass mc = new MyClass();
mc.Method(); //compiles just fine
BFree
  • 102,548
  • 21
  • 159
  • 201
3

An implicit interface implementation is where you have a method with the same signature of the interface.

An explicit interface implementation is where you explicitly declare which interface the method belongs to.

interface I1
{
    void implicitExample();
}

interface I2
{
    void explicitExample();
}


class C : I1, I2
{
    void implicitExample()
    {
        Console.WriteLine("I1.implicitExample()");
    }


    void I2.explicitExample()
    {
        Console.WriteLine("I2.explicitExample()");
    }
}

MSDN: implicit and explicit interface implementations

Yochai Timmer
  • 48,127
  • 24
  • 147
  • 185
  • What is the difference in use then? Why would you prefer one above another? – Killerwes May 26 '11 at 14:12
  • When you have 2 interfaces with the same method signature, you could explicitly declare 2 methods that implement the method for each. Or if you want it to be clear that a method is there for implementing the interface you can explicitly declare so. Implicit declaration exists because mostly you don't really need to declare explicitly, and the interface implementation can be assumed. – Yochai Timmer May 26 '11 at 14:15
2

Explicit simply means that you specify the interface, implicit means that you don't.

For example:

interface A
{
  void A();
}

interface B
{
  void A();
}

class Imp : A
{
    public void A() // Implicit interface implementation
    {

    }
}

class Imp2 : A, B
{
    public void A.A() // Explicit interface implementation
    {

    }

    public void B.A() // Explicit interface implementation
    {

    }
}
Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • So, public void A in the class Imp is an implicit interface, because it is created within that class, and doesn't have a reference to interface A (not A.A() ). If it would've been A.A() It would have been an explicit interface? Whats the difference in use then? – Killerwes May 26 '11 at 14:10
  • @Killerwes - That's the _implementation_ side, and as such your are correct. I hase to be explicit in `Imp2` as both interfaces declare the same member. In calling code you need the correct interface reference in order to call the different explicit implementations of `Imp2`. – Oded May 26 '11 at 14:17
1

Also, if you want to know why explicit implementation exists, that's because you may implement the same method (name and signature) from more than one interface; then if you need different functionality, or just the return types are different, you can't achieve this through simple overloading. then you'll have to use explicit implementation. One example is that List<T> implements IEnumerable and IEnumerable<T> and both have GetEnumerator() but with different return values.

Alireza
  • 5,421
  • 5
  • 34
  • 67