We pay for user submitted tutorials and articles that we publish. Anyone can send in a contribution
Learn MoreWhenever we need to implement an interface in C#, two options pops up (click Ctrl+’.'):

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
raises 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 :
.NetC#DiamondExplicitImplementationImplicitInterface Copyright © 2012 Dev102.com
Breeze : Designed by Amit Raz and Nitzan Kupererd
rams
Said on April 8, 2008 :
Thanks for explaining the differences. Nice article.
dario
Said on May 29, 2008 :
muy buena ilustracion
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.
Bishoy
Said on November 29, 2008 :
Nice post,thanks
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.
Ibrahim
Said on March 28, 2009 :
Thanks, nice post
meeran
Said on November 25, 2009 :
nice articals plz continue ur work sir..
Asad Ali Butt
Said on March 16, 2010 :
Good one mate, really helped me to understand the difference better. Do appriciate
Rahul More
Said on August 19, 2010 :
Excellent …….. !!!!
Nice article,helped me a lot to understand the difference.
Please keep up the good work
Bala Sakthis
Said on November 17, 2010 :
Hi Shahar,
Nice post. Understood the concept quickly
Thanks!
Bala
LEGENDS
Said on July 3, 2011 :
Nice short explanation of impl. and expl. interfaces!