We know how to create an interface as in the following:
- interface IFirstInterface {
- }
- interface IFirstInterface {
- void InterfaceMethod();
- }
In my project I also have a program class as in the following:
- class Program {
- static void Main(string[] args) {
- }
- }
- class Program: IFirstInterface {
- static void Main(string[] args) {
- }
- }
- class Program: IFirstInterface {
- public void InterfaceMethod() {
- Console.WriteLine("The first interface method");
- }
- static void Main(string[] args) {
- }
- }
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:
- class Program: IFirstInterface {
- public void InterfaceMethod() {
- Console.WriteLine("The first interface method");
- }
- static void Main(string[] args) {
- Program p = new Program();
- p.InterfaceMethod();
- }
- }

Now let's say I have another interface ISecondInterface and it happens that even this interface has a method with the same name, InterfaceMethod.
- interface ISecondInterface {
- void InterfaceMethod();
- }
- class Program: IFirstInterface,ISecondInterface {
- }
- class Program: IFirstInterface,ISecondInterface {
- public void InterfaceMethod(){
- Console.WriteLine("The first interface method");
- }
- }
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.

But then when you actually try to invoke the method, I mean when we say p.InterfaceMethod() as in the following:
- static void Main(string[] args) {
- Program p = new Program();
- p.InterfaceMethod();
- }
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
- public void InterfaceMethod(){
- Console.WriteLine("The first interface method");
- }
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.
- void IFirstInterface.InterfaceMethod(){
- Console.WriteLine("The first interface method");
- }

Let's provide the implementation for the interface too.
- class Program: IFirstInterface,ISecondInterface {
- void IFirstInterface.InterfaceMethod(){
- Console.WriteLine("The first interface method");
- }
- void ISecondInterface.InterfaceMethod() {
- Console.WriteLine("The second interface method");
- }
Invoking an explicitly implemented interface members from our main class
Before using an explicit implementation we did the following:
- static void Main(string[] args) {
- Program p = new Program();
- p.InterfaceMethod();
- }

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:

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.
- static void Main(string[] args) {
- Program p = new Program();
- ((IFirstInterface)p).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.
- static void Main(string[] args) {
- Program p = new Program();
- ((IFirstInterface)p).InterfaceMethod();
- ((ISecondInterface)p).InterfaceMethod();
- }

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.
- static void Main(string[] args) {
- IFirstInterface iFirst = new Program();
- ISecondInterface iSecond = new Program();
- iFirst.InterfaceMethod();
- iSecond.InterfaceMethod();
- }
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.

Reyaad El KomyPosted Mar 8, 2018, 9:06 AM
Very nice and good work really
Gowtham RajamanickamPosted Mar 18, 2015, 3:38 AM
good show...
Pramod LawatePosted Jan 9, 2015, 12:50 PM
Very nice way represent the explicit interface Harpreet.......Grate work
Dinesh BeniwalPosted Jan 9, 2015, 11:50 AM
Good work Harpreet Singh
rahul rathorePosted Jan 9, 2015, 2:38 AM
nice article
Guest UserPosted Jan 9, 2015, 2:02 AM
Harpreet Singh, i never thought about this challenge of same method names across two interfaces. Good to read your article!
Michal HabalcikPosted Jan 9, 2015, 1:03 AM
good start. Just one thing to add, interfaces are being implemented, not inherited.
Manish Kumar ChoudharyPosted Jan 8, 2015, 11:30 PM
Nice start..