might someone have a quick answer for me...
in the following entirely useless code, under 'class DuplicateInterfaceClass : MyInterface1, MyInterface2'.
Why can't I explicitly write "public string MyInterface2.P()"?
yet "public string P()" and "string MyInterface2.P()" work.
I understand that all interface methods (properties, etc.) are implicitly "public" by default, but my attempt to be explicit in the inheriting class results in an "error CS0106: The modifier 'public' is not valid for this item".
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());
}
}