Implicit versus Explicit Interface implementation (C#)

Unlike C++, in C# we can implement an interface implicilty as well as explicitly. There are many prons and cons of using the explicit interface implementation instead of regular implicit implementation.

I’ll first show you the implicit implmentation of an interface in an class, and then will talk about the use of explicit implementation and its differences with implicit implementation.

Implicit implementation of an Interface:

public interface ITest

{

    //method declaration

    void TestMethod();

}

public class TestClass : ITest

{

    //Implicit Interface Implementation

    void TestMethod()

    {

        //Method Implementation

    }

}

Now to call this method on the object of TestClass,

class Program

{

    static void Main()

  { 

     TestClass testObj = new  TestClass();

     testObj.TestMethod(); 

  }

}

As we have implemented interface implicitly, TestMethod is called on the object of the TestClass.

Also we make any change to the method declared in the interface, like change in the paramters, we will get the compile time error saying “method TestMethod of ITest interface has not implemented”.

But if we implement this same method of the ITest interface as Explicit Implementation as:

public class TestClass : ITest

{

    //Explicit Interface Implementation

    void ITest.TestMethod()

    {

        //Method Implementation

    }

}

As shown above the name of the interface (ITest) has been appended to the method TestMethod. Now to call this method on the object of TestClass it must first be casted to the interface otherwise compile error will be there.

    static void Main()

  { 

     ITest testObj = new  TestClass();

     testObj.TestMethod(); 

  }

In this case of explicit implementation, we make any change in the method signature etc in the interface, we will be told on compilation that method ‘TestMethod’ does not match with that in the interface.

Apart from the difference in implementation & way of using the method in Implicit & Explicit implementation there are few more differences which may be the deciding factor for using an implicit or explicit implementation of an interface as per the requirement.

  • Multiple Inheritence (Solving DOD problem): As C# does not support for the multiple inheritnce due to the well know Diamond of Death problem which takes place when there are same method existing in more than one classes being derived by another class.

         So this problem can be avioded while implementing the multiple inheritence by means of achieving MI using the interfaces. Now again if more than one interface contains the same method, again calling the method on the object of the class implementing those interfaces will be in trouble.

So here Explicit implementation comes very effective. If we have implemented the method in the derived class explicitly (by Interface name), calling the method will be done specifying the name of the interface with the name of the mehod being called, so correct/required method will be called.

interface IMyOtherInterface

{

    void TestMethod();

}

class DerivedClass : IMyInterface, IMyOtherInterface

{

    void IMyInterface.TestMethod()

    {

        // Code for TestMethod of the IMyInterface

    }

    void IMyOtherInterface.ApproveLoan()

    {

        // Code for TestMethod of the IMyOtherInterface

    }

}

  • Private versus Public: As by default all the methods of an interface are public so in case of the implicit implementation method must be specified as ‘public‘ which on the other hand in case of explicit implementation, it can be ‘private’ and thus in later case it would not be available on the intellsense for the object of the class.
  • No virtual method: Explict methods can not be marked as ‘virtual‘ to make them available for the dervied classes to override them. In case of implicit mehtod, they can be virtual.
  • No Abstract explicit method: Explicit methods can not be abstract whereas implict methods can be.
Advertisements
This entry was posted in .Net 2.0. Bookmark the permalink.

22 Responses to Implicit versus Explicit Interface implementation (C#)

  1. Naveen Sikri says:

    Good work Bro.. 😉

  2. IamStalker says:

    Great Post mate, thank you very much!

    I loved this very much please continue posting such a nice post’s.

    Thank you…

  3. Thanks for your precious comments !

    Munish

  4. colin says:

    GOOOOOOOOOD POST.

  5. Amita Vashisht says:

    Very Goooooooooood post.

  6. Thanks for your appreciation, Amita.

  7. Good one mate, really helped me understand the issue well. do appreciate

  8. Thanks for your comments, Asad.

  9. Kishore says:

    Excellent post, very nicely explained.
    Keep posting more.

    Thanks,
    Kishore

  10. ramya says:

    explanation of concept is too clear. . . .thank u…

  11. Pompey says:

    Very informative and simple ,to understand. Thanks a lot Munish

  12. Neha says:

    As you said that in “Private versus Public”, If method of interface is private then what the use of it?

    Can we define virtual or abstract method in interface?

    thanks in advance….

  13. Vijay says:

    Excellent Post!!! Really a nice one..

  14. swamy sashidhar says:

    good and simple to understand thanks munish……..

  15. Kamesh says:

    Thanks for the Post, it is worthful

  16. Sushil Kumar says:

    Private versus Public: As by default all the methods of an interface are public so in case of the implicit implementation method must be specified as ‘public‘ which on the other hand in case of explicit implementation, it should be ‘private’ otherwise it would throw compile time error.

  17. Robert says:

    Very clear, short and nice explained post. Thank you.

  18. Pat M says:

    Great Munish. Thanks for explaining the subtle differences between explicit and implicit implementations of an interface – very helpful.

  19. Nisha s says:

    nice post manish 🙂 mwaha

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s