Whenever 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.
We pay for user submitted tutorials and articles that we publish. Anyone can send in a contribution
Learn More
rams Said on Apr 8, 2008 :
Thanks for explaining the differences. Nice article.
dario Said on May 29, 2008 :
muy buena ilustracion
Andriy Said on Aug 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 Nov 29, 2008 :
Nice post,thanks
Hamali Said on Dec 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 Mar 28, 2009 :
Thanks, nice post