Implicit and Explicit Interface Implementation in C#: When and Which to Use?
Hello everyone! I just started learning C# and came across the topic of interfaces. It seems clear: an interface is a contract, and a class implements it. But then I saw that interfaces can be implemented in two ways: implicitly and explicitly. And I got confused.
Here's an example:
public interface IWorker
{
void Work();
}
public class Employee : IWorker
{
// Implicit implementation
public void Work()
{
Console.WriteLine("Employee works");
}
}
Everything works. But I read that you can also do it like this:
public class Manager : IWorker
{
// Explicit implementation
void IWorker.Work()
{
Console.WriteLine("Manager works");
}
}
And now, if I write:
Manager m = new Manager();
m.Wor No answers yet.
Log in to leave an answer
Blogs