Whenever we need to implement an interface in C#, two options pops up (click Ctrl+’.'):

implicit_explicit_impl

Implicit vs explicit interface implementation, what shall be selected? before making a decision, lets understand what is the difference between those two.

Solving the diamond problem:

The diamond problem is related to object oriented languages that allow multiple inheritance. The problem Diamond_inheritanceraises when there is base class A, classes B and C derived from A and class D derived from B and C. B and C, both have a method “Foo”, that has been overridden differently. If this method is called in D, then via which class is it inherited: B or C?

OK, take a breath and digest this data…

As we know C# does not support multiple inheritance of classes, but it does support multiple inheritance of interfaces. Therefore, the same ambiguity may arise unless explicit interface implementation is used. Going explicitly, we can have different implementations to the same method. The problem is solved…

interface IRegularAccess
{
    void ApproveLoan();
}           

interface IAdministratorAccess
{
    void ApproveLoan();
}           

class BankDatabaseProxy : IRegularAccess, IAdministratorAccess
{
    void IRegularAccess.ApproveLoan()
    {
        // Code for regular access approve loan.
    }           

    void IAdministratorAccess.ApproveLoan()
    {
        // Code for administrator access approve loan.
    }
}

A good example of such a usage is a bank, where one class implements 2 interfaces to the data-base proxy: IRegularAccess and IAdministratorAccess.It is clear that the ApproveLoan method shall not be implemented in the same manner for both cases.

Private and Public issues:

In implicit implementation, the public keyword is attached to the method (otherwise, compilation error). It is the opposite with explicit implementation (where the private keyword is used). This is how the implicit implementation looks with our example:

class BankDatabaseProxy : IRegularAccess, IAdministratorAccess
 {
     public void ApproveLoan()
     {
         // Insert your code.
     }
 }

The result is that the explicit implementation keeps the interface members out of the class (and out of the Intellisense too). Again, it is much easier to demonstrate this issue in code:

// Does not compile because of explicit implementation.
BankDatabaseProxy bankDB = new BankDatabaseProxy();
bankDB.ApproveLoan();           

// This is good.
IAdministratorAccess adminAccessToBankDB = new BankDatabaseProxy();
adminAccessToBankDB.ApproveLoan();

No Virtual for Explicit:

Explicit method implementation can not be virtual, whereas an implicit method can. Child classes won’t be able to override an explicit method.

No Abstract for Explicit:

Explicit method implementation can not be abstract, whereas an implicit method can. Class that use the explicit implementation can not be abstract!

This is it, I hope that you will find that this post helpful. Enjoy and feel welcome to leave your comments.

Tags :

14 Responses to “4 Key Differences Between Implicit and Explicit Interface Implementation”


  1. rams

    Said on April 8, 2008 :

    Thanks for explaining the differences. Nice article.

  2. dario

    Said on May 29, 2008 :

    muy buena ilustracion

  3. Andriy

    Said on August 1, 2008 :

    No Virtual for Explicit:
    No Abstract for Explicit:
    ………………………
    I think this is not the issue of imlicit vs explicit.

    Any private members can’t be visrtual or abstract, not only explicit interface method implementation.

  4. Bishoy

    Said on November 29, 2008 :

    Nice post,thanks

  5. Hamali

    Said on December 18, 2008 :

    Nice post. So to point out another benefit of explicit interface implementation: if you need to hide methods from being visible outside the assembly, then you cannot use implicit implementation.

  6. Ibrahim

    Said on March 28, 2009 :

    Thanks, nice post

  7. meeran

    Said on November 25, 2009 :

    nice articals plz continue ur work sir..

  8. Asad Ali Butt

    Said on March 16, 2010 :

    Good one mate, really helped me to understand the difference better. Do appriciate

  9. Rahul More

    Said on August 19, 2010 :

    Excellent …….. !!!!
    Nice article,helped me a lot to understand the difference.
    Please keep up the good work

  10. Bala Sakthis

    Said on November 17, 2010 :

    Hi Shahar,
    Nice post. Understood the concept quickly

    Thanks!
    Bala

  11. LEGENDS

    Said on July 3, 2011 :

    Nice short explanation of impl. and expl. interfaces!

3 Trackback(s)

  1. Apr 9, 2008: Reflective Perspective - Chris Alcock » The Morning Brew #69
  2. Apr 23, 2008: WPF DATATEMPLATE FOR INTERFACES? NOT SUPPORTED | Dev102.com
  3. May 2, 2008: BLOG STATS FOR APRIL 2008 | Dev102.com

Post a Comment