我在阅读有关隐式或显式接口方法的实现,但我仍不明白它是如何工作的以及有哪些好处。接口实现:隐式与显式
具有代码:
interface InterfaceOne
{
void MethodOne();
}
class ClassOne : InterfaceOne
{
public void MethodOne()
{
Console.WriteLine("hello from the class method");
}
void InterfaceOne.MethodOne()
{
Console.WriteLine("hello from the interface method");
}
}
而且从main方法的代码:
var c1 = new ClassOne();
c1.MethodOne();
InterfaceOne i1 = new ClassOne();
i1.MethodOne();
Console.ReadLine();
,这里是输出:从类方法
你好
你好从接口方法
我的问题:
为什么我没有有两种方法具有相同的名称和签名类的错误?
当我使用var关键字时,编译器如何选择调用哪个方法?
有什么好处?
@MarcinJuraszek它不是重复的,因为你提到的问题没有回答我的第二个问题 –