誰かが私に簡単に答えてくれるかもしれません...
次のまったく役に立たないコードで、「class DuplicateInterfaceClass : MyInterface1, MyInterface2」の下にあります。
「public string MyInterface2.P()」と明示的に記述できないのはなぜですか?
それでも「パブリック文字列 P()」と「文字列 MyInterface2.P()」は機能します。
すべてのインターフェイス メソッド (プロパティなど) が既定で暗黙的に "パブリック" であることは理解していますが、継承クラスで明示的にしようとすると、"エラー CS0106: 修飾子 'public' はこのアイテムに対して有効ではありません" が発生します。
using System;
interface MyInterface1
{
void DuplicateMethod();
// interface property
string P
{ get; }
}
interface MyInterface2
{
void DuplicateMethod();
// function ambiguous with MyInterface1's property
string P();
}
// must implement all inherited interface methods
class DuplicateInterfaceClass : MyInterface1, MyInterface2
{
public void DuplicateMethod()
{
Console.WriteLine("DuplicateInterfaceClass.DuplicateMethod");
}
// MyInterface1 property
string MyInterface1.P
{ get
{ return ("DuplicateInterfaceClass.P property"); }
}
// MyInterface2 method
// why? public string P()...and not public string MyInterface2.P()?
string MyInterface2.P()
{ return ("DuplicateInterfaceClass.P()"); }
}
class InterfaceTest
{
static void Main()
{
DuplicateInterfaceClass test = new DuplicateInterfaceClass();
test.DuplicateMethod();
MyInterface1 i1 = (MyInterface1)test;
Console.WriteLine(i1.P);
MyInterface2 i2 = (MyInterface2)test;
Console.WriteLine(i2.P());
}
}