What is implicit and explicit implementation of interfaces? In which scenario it uses? why its need ? in dot net
2 Answers
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.
Comments
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