Explicit Implementation in C# Interfaces



If a class implements two interfaces that contain a member with the same signature, then implementing that member on the class will cause both interfaces to use that member as their implementation.

It's possible to implement an interface member explicitly—creating a class member that is only called through the interface, and is specific to that interface

Example

interface ICar{
   void display();
}
interface IBike{
   void display();
}
class ShowRoom : ICar, IBike{
   void ICar.display(){
      throw new NotImplementedException();
   }
   void IBike.display(){
      throw new NotImplementedException();
   }
}
class Program{
   static void Main(){
      Console.ReadKey();
   }
}
Updated on: 2020-08-04T07:12:33+05:30

333 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements