Here we will see how to implement an Interface implicitly and explicitly in CSharp.
Implicit Implementation
Let's take an example to understand, what implicit and explicit implementation of interface is. I am going to create an interface named with “IEmployee” and a class named with “Employee”.
When the class implements the interface member, it does not need to specify the interface name with the member name. We can simply implement it.
interface IEmployee
{
void Greeting();
}
public class Employee : IEmployee
{
public void Greeting()
{
Console.WriteLine("This is implicitly implementation of Interface");
}
}
See in the above code, when class implement the interface method “Greeting()”, it doesn’t specify the interface name.
When we need to access this method, we can create the instance of the class and when we pass object, then it gives the name of implemented method. See the following code.
class Program
{
static void Main(string[] args)
{
Employee emp = new Employee();
emp.Greeting();
Console.ReadLine();
}
}
Explicit Implementation
Sometimes, we get a situation where a class implemented the multiple interface and those interfaces contain the same name method. When class is going to implement the interface members, they don’t differentiate between the method, because of the method name or same in both the interfaces.
For resolving this issue we need to specify the name of the interface at the time of implementation explicitly.
Let's take an example to understand what explicit implementation of interface is. I am going to create one more interface “IManager” which will be implemented by Employee class. IManager contains the same name method as IEmployee contains. So, the question is that how we will implement the IManager member method. Have a look in the following example.
using System;
namespace ConsoleDemo
{
interface IEmployee
{
void Greeting();
}
interface IManager
{
void Greeting();
}
public class Employee : IEmployee, IManager
{
void IEmployee.Greeting()
{
Console.WriteLine("This is greeting for Employee");
}
void IManager.Greeting()
{
Console.WriteLine("This is greeting for Manager");
}
}
class Program
{
static void Main(string[] args)
{
IEmployee emp = new Employee();
emp.Greeting();
IManager man = new Employee();
man.Greeting();
Console.ReadLine();
}
}
}
Here you should notice the following two things.
Access modifier has been removed from the Greeting() method.
The Greeting() method name showed only with the interface name [explicitly].
One more thing, you have noticed here that when we need to access the implemented method using instance of class, we use the interface name for creating the instance of the class.
IEmployee emp = new Employee();
emp.Greeting();
IManager man = new Employee();
man.Greeting();
So, we see that when you are using the two or more interfaces which have same name method and they implemented by the same class, so method signature in the interface clashes. Therefore, in that scenario you need to specify the name of the interface at the time of implementation.
Thanks for reading and I hope you enjoyed this article.