What is the implicit implementation of the interface and when to use implicit implementation of the interface in C#?


C# interface members can be implemented explicitly or implicitly.

Implicit implementations don't include the name of the interface being implemented before the member name, so the compiler infers this. The members will be exposed as public and will be accessible when the object is cast as the concrete type.

The call of the method is also not different. Just create an object of the class and invoke it.

Implicit interface cannot be used if there is same method name declared in multiple interfaces

Example

interface ICar {
   void displayCar();
}
interface IBike {
   void displayBike();
}
class ShowRoom : ICar, IBike {
   public void displayCar() {
      throw new NotImplementedException();
   }
   public void displayBike() {
      throw new NotImplementedException();
   }
}
class Program {
   static void Main() {
      ICar car = new ShowRoom();
      IBike bike = new ShowRoom();
      Console.ReadKey();
   }
}

Updated on: 05-Aug-2020

617 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements