Home  >  Article  >  Backend Development  >  What is implicit implementation of interfaces and when to use implicit implementation of interfaces in C#?

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

WBOY
WBOYforward
2023-09-11 22:17:06936browse

什么是接口的隐式实现以及何时在 C# 中使用接口的隐式实现?

C# Interface members can be implemented explicitly or implicitly.

Implicit implementation does not include the name of the interface being implemented before the member name, so the compiler infers this. These members will be exposed as public and accessible when the object is converted to a concrete type. The invocation of the

method is no different. Just create an object of this class and call it.

If the same method name is declared in multiple interfaces, you cannot use an implicit interface

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();
   }
}

The above is the detailed content of What is implicit implementation of interfaces and when to use implicit implementation of interfaces in C#?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete