Explicit Interfaces in C#

We know how to create an interface as in the following:

  1. interface IFirstInterface {  
  2.   
  3. }  
To add a method we do the following:
  1. interface IFirstInterface {  
  2. void InterfaceMethod();  
  3. }  
We know that interface members cannot have access modifiers and they are public by default. They can only have the declaration, no implementation.

In my project I also have a program class as in the following:
  1. class Program {  
  2. static void Main(string[] args) {  
  3.   
  4. }  
  5. }  
So, if we inherit the IFirstInterface interface in our program class then this class must provide an implementation for all the members present in that specific interface.
  1. class Program: IFirstInterface {  
  2. static void Main(string[] args) {  
  3.   
  4. }  
  5. }  
So, let's provide an implementation.
  1. class Program: IFirstInterface {  
  2. public void InterfaceMethod() {  
  3. Console.WriteLine("The first interface method");  
  4. }  
  5. static void Main(string[] args) {  
  6.   
  7. }  
  8. }  
Note

When we provide an implementation for the interface members in our class, we don't get any intellisense. So, be sure to write the codes correctly.

Now, if we want to invoke this method, we can create an instance of our program class in our main method as in the following:
  1. class Program: IFirstInterface {  
  2. public void InterfaceMethod() {  
  3. Console.WriteLine("The first interface method");  
  4. }  
  5. static void Main(string[] args) {  
  6. Program p = new Program();  
  7. p.InterfaceMethod();  
  8. }  
  9. }  
interface method

Now let's say I have another interface ISecondInterface and it happens that even this interface has a method with the same name, InterfaceMethod.
  1. interface ISecondInterface {  
  2. void InterfaceMethod();   
  3. }  
And our class is inheriting from both of the interfaces, IFirstInterface and ISecondInterface as in the following:
  1. class Program: IFirstInterface,ISecondInterface {  
  2. }  
Our class has provided an implementation for InterfaceMethod as in the following:
  1. class Program: IFirstInterface,ISecondInterface {  
  2. public void InterfaceMethod(){  
  3. Console.WriteLine("The first interface method");  
  4. }  
  5. }  
Now which one of the methods do you think our class has implemented? Is it IFirstInterface InterfaceMethod or ISecondInterface InterfaceMethod?

We don't know about that.

So, there is an ambiguity. But Visual Studio (.NET) thinks that the program class has implemented both of the interface methods.

Even if we build our project we will not get an error.

interface

But then when you actually try to invoke the method, I mean when we say p.InterfaceMethod() as in the following:
  1. static void Main(string[] args) {  
  2. Program p = new Program();  
  3. p.InterfaceMethod();  
  4. }  
Which method are we calling? Is it IFirstInterface or ISecondInterface method?

There is no clarity on that.

So, if a class inherits from two or more interfaces and those methods has the same name then you can use explicit interface implementation.

Using explicit interface implementation we would easily specify which method we are calling. If you want to explicitly implement the members of an interface then we need to make some changes first.

Before explicit implementation
  1. public void InterfaceMethod(){  
  2. Console.WriteLine("The first interface method");  
  3. }  
After explicit implementation

In explicit implementation there are two changes we need to do. One, we cannot use the access modifier and second, we need to use interface_name period(.) the_method_name.
  1. void IFirstInterface.InterfaceMethod(){  
  2. Console.WriteLine("The first interface method");  
  3. }  
Now, if you try to build your solution you will get a compile time error because we have only implemented the IFirstInterface method.

error

Let's provide the implementation for the interface too.
  1. class Program: IFirstInterface,ISecondInterface {  
  2. void IFirstInterface.InterfaceMethod(){  
  3. Console.WriteLine("The first interface method");  
  4. }  
  5. void ISecondInterface.InterfaceMethod() {  
  6. Console.WriteLine("The second interface method");  
  7. }  
So, now our class has implemented both of the interfaces members.

Invoking an explicitly implemented interface members from our main class

Before using an explicit implementation we did the following:
  1. static void Main(string[] args) {  
  2. Program p = new Program();  
  3. p.InterfaceMethod();  
  4. }  
Output

Output

But when we use explicit implementation, we will not be able to invoke those methods from our class reference variable.

Meaning you cannot say p.name_of_the_interface_method as in the following:

code

We will not get InterfaceMethod suggestion in the intellisense.

So, how is it possible to invoke the method?

To invoke these methods we need to type-cast our class object reference variable to an interface.
  1. static void Main(string[] args) {  
  2. Program p = new Program();  
  3. ((IFirstInterface)p).InterfaceMethod();  
  4. }  
interfacemethod

We will now also see in the intellisense from where this InterfaceMethod comes from.

We need to do the same for the ISecondInterface too.
  1. static void Main(string[] args) {  
  2. Program p = new Program();  
  3. ((IFirstInterface)p).InterfaceMethod();  
  4. ((ISecondInterface)p).InterfaceMethod();  
  5. }  
Run the program.

Run the program

There is also another way to do the same thing. If you want don't want to type-caste the reference variable to an interface then you can do the following.
  1. static void Main(string[] args) {  
  2. IFirstInterface iFirst = new Program();  
  3. ISecondInterface iSecond = new Program();  
  4. iFirst.InterfaceMethod();  
  5. iSecond.InterfaceMethod();  
  6. }  
From inheritance we learned that we can have a base-reference variable pointing to our child class.

Important Note

When a class explicitly implements an interface member, the interface member can no longer be accessed using a class reference variable but only using an interface reference variable.

Access modifiers are not allowed on explicitly implemented interface members.