Normally an interface can be implemented in a class in a normal way, implicit implementation. Sometimes, we may have the same method name in different interfaces. If this is the case, we need to do an explicit implementation of the interface method. The main use of an explicit implementation is to avoid the ambiguities between the class or method name. In order to do that the interface name is put before that interface's method. The following example shows the implicit and explicit implementation:
  1. namespace ConsoleApplicationC
  2. {
  3. // 2 interfaces with same method name
  4. interface IintegerAdd
  5. {
  6. void Add();
  7. }
  8. interface IfloatAdd
  9. {
  10. void Add();
  11. void Multiply();
  12. }
  13. // We implement Both interfaces
  14. class ArithmeticOperation : IintegerAdd, IfloatAdd
  15. {
  16. // Implicit Implementation : There is no name collision so we can implement implicitly
  17. // NOTE : public modifier MUST here
  18. public void Multiply()
  19. {
  20. float a = 1.5f, b = 2.5f;
  21. Console.WriteLine("Float Multiplication is:" + a * b);
  22. }
  23. // Explicit Implementation : Explicitly tell the compiler that we implement the interface IintegerAdd
  24. void IintegerAdd.Add()
  25. {
  26. int a = 10, b = 20;
  27. Console.WriteLine("Integer Addition Is:" + (a + b));
  28. }
  29. // Explicit Implementation : Explicitly tell the compiler that we implement the interface IfloatAdd
  30. void IfloatAdd.Add()
  31. {
  32. float a = 1.5f, b = 2.5f;
  33. Console.WriteLine("Float Addition Is:" + (a + b));
  34. }
  35. }
  36. class Program
  37. {
  38. static void Main(string[] args)
  39. {
  40. ArithmeticOperation objA = new ArithmeticOperation();
  41. IintegerAdd iobj = (IintegerAdd)objA;
  42. iobj.Add();
  43. IfloatAdd fobj = (IfloatAdd)objA;
  44. fobj.Add();
  45. Console.ReadKey();
  46. }
  47. }
  48. }
Output:

Integer Addition Is:30
Float Addition Is:4
The purpose of "Explicit" implementation of an Interface
There are 2 purposes of explicit implementation of an interface. First, to avoid name collision between interface methods. That is, if you are going to create a class library, there may be a chance to use the same name in several places. At that time "explicit" implementation comes to the rescue. Secondly, you cannot access that implemented method using the object of the class directly. Instead you typecast it as an interface reference, then you can access it. This is because the C# compiler is unable to determine which one the user wants to call.
In another way, you need to add a "public" access specifier to the interface's implemented method in a class. But if you "explicitly" implement the interface then you can change this one. That means, it will look like a private method. But with one exception, you can access that explicit implemented method using the reference of the interface. Please have a look at the following code for a detailed explanation.
  1. interface ISample
  2. {
  3. void method1();
  4. void method2();
  5. }
  6. interface IAnotherSample
  7. {
  8. void method1();
  9. }
  10. class A : ISample, IAnotherSample
  11. {
  12. // Explicit implementation avoid name collision
  13. void ISample.method1()
  14. {
  15. }
  16. void IAnotherSample.method1()
  17. {
  18. }
  19. // Implicit implementation
  20. public void method2()
  21. {
  22. }
  23. }
  24. // Inherit Class A
  25. class B : A
  26. {
  27. // Create Object for class A
  28. A obj = new A();
  29. B()
  30. {
  31. // You can call method2
  32. obj.method2();
  33. // Error: You cannot call method1 Since Explicit implementation
  34. //obj.method1();
  35. // But you can access it by Interface Reference
  36. ((ISample)obj).method1();
  37. }
  38. }
Finally, one more point or little pros is that an explicitly implemented interface's methods are not listed in the Visual Studio IDE's intellisense.
Happy Coding!