什么是接口的隐式实现?在 C# 中何时使用接口的隐式实现?
csharpserver side programmingprogramming更新于 2025/10/2 8:07:17
C# 接口成员可以显式或隐式实现。
隐式实现不会在成员名称前包含要实现的接口名称,因此编译器会推断这一点。成员将公开为公共成员,并在对象转换为具体类型时可访问。
方法的调用也一样。只需创建该类的对象并调用它即可。
如果在多个接口中声明了相同的方法名称,则不能使用隐式接口。
示例
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();
}
}

