我在閱讀有關隱式或顯式接口方法的實現,但我仍不明白它是如何工作的以及有哪些好處。接口實現:隱式與顯式
具有代碼:
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它不是重複的,因爲你提到的問題沒有回答我的第二個問題 –