Tuesday, January 7, 2014

Implicit Vs Explicit Interface implementation in C#


If you want to understand the difference between implicit and explicit interface and confused which interface to use when, then you are at right place.

Implicit Interface

Implicit interface allows to access methods and properties of an interface as they are part of implementing class.

Explicit Interface

Explicit interface allows to access methods and properties of an interface only when you are treating an Interface.

Let’s understand the above explanation with the help of example:    

Example:

Manager is a class that implements IEmployee and IDepartment interface. Both IEmployee and IDepartment has a method Display(). IEmployee’s method Display() is implemented implicitly and IDepartment’s method is implemented explicitly.

public interface IEmployee
       {
              void Display();
       }

public interface IDepartment
       {
              void Display();
       }

public class Manager : IEmpoyee, IDepartment
       {
              public void Display()
              {
                     Console.WriteLine("IEmployee.Display().");
              }
void IDepartment.Display()
              {
                     Console.WriteLine("IDepartment.Display().");
              }
              public void GetReport()
              {
                     Console.WriteLine("Generate report.");
              }
       }
class Program
       {
              static void Main(string[] args)
              {
                     Manager manager = new Manager();
                     manager.Display();
              }
       }
Difference between implicit and Explicit interface implementation

·         No need to specify interface name with method in implicit interface, where as it is required to specify interface name with method implementation in explicit interface.

o    public void Display() vs void IDepartment.Display()

·         Access modifier is required with method name and it must be public, even protected will not work; whereas in Explicit, no access modifier is required.

·         Implicit implementation allows making method as virtual and abstract; same is not true with explicit interface implementation.

When to use implicit and Explicit interface implementation

·         Do not use explicit members as a security boundary. They can be called by any client who cast an instance to the interface.

·         Do use explicit members to hide implementation details:

o   In explicit interface implementation, IDepartment.Display() method will not appear in class Intellisense list, Below digram shows implecit Display method of IEmployee interface.

o   In above example if you want to invoke Display method of IDepartment interface, use below code:

 
·         Do use explicit members to approximate private interface implementations.

·         Do expose an alternative way to access any explicitly implemented members that subclasses are allowed to override. Use the same method name unless a conflict would arise.

5 comments: