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

CsharpServer Side ProgrammingProgramming

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();
   }
}
raja
Published on 05-Aug-2020 13:28:06
Advertisements