1

What is implicit and explicit implementation of interfaces? In which scenario it uses? why its need ? in dot net

leppie
  • 115,091
  • 17
  • 196
  • 297
Red Swan
  • 15,157
  • 43
  • 156
  • 238
  • possible duplicate of http://stackoverflow.com/questions/143405/c-interfaces-implicit-and-explicit-implementation – derek Aug 30 '10 at 11:44

2 Answers2

1

in the explicit implementation, you use both the name of the interface and the name of the method you're implementing . It allows you to use several methods with the same name in your class (for instance if the class implements several interfaces)

public interface I
{
  void A();
}

public class myClass: I
{
  public void I.A()
  {
    // do some stuff
  }
}

read this aricle, it explains quite clearly why you could need explicit implementation: http://blogs.msdn.com/b/mhop/archive/2006/12/12/implicit-and-explicit-interface-implementations.aspx

pierroz
  • 7,653
  • 9
  • 48
  • 60
1

Implicit implementation is when you implement the interface member without specifying the interface name at the same time.

public interface IFoo 
{
    void Bar();
}

public class ClassA : IFoo 
{
    //this is implicit
    public void Bar() 
    {

    }
}

public class ClassB : IFoo 
{
    //this is explicit:
    void IFoo.Bar()
    {

    }
}

You need explicit implementation when you implement two (or more) interfaces that have a function/property with the same name and signature. In this case the compiler needs to be specifically told which implementation belongs to which interface.

slugster
  • 49,403
  • 14
  • 95
  • 145