1

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());
    }
}
svick
  • 236,525
  • 50
  • 385
  • 514
user1229895
  • 2,259
  • 8
  • 24
  • 26
  • By the way it's probably a duplicate with http://stackoverflow.com/questions/1253266/why-explicit-implementation-of-a-interface-can-not-be-public and http://stackoverflow.com/questions/2669031/compilation-error-the-modifier-public-is-not-valid-for-this-item-while-crea – Julien Mar 02 '12 at 08:17
  • btw, I realize I shouldve said "implement" the interface not "inherit" – user1229895 Mar 02 '12 at 08:19
  • I think you are right Julien...oh and I can add "void" to 'DuplicateMethod()' because there is no ambiguity.... – user1229895 Mar 02 '12 at 08:20

1 Answers1

1

I have this explicit message from Resharper : "The modifier 'public' is not valid for explicit interface implementation."

But you can do that :

class DuplicateInterfaceClass : MyInterface1, MyInterface2
{
 public void DuplicateMethod()
 {
  Console.WriteLine("DuplicateInterfaceClass.DuplicateMethod");
 }

 string MyInterface1.P
 { get { return "DuplicateInterfaceClass.P"; } }

 string MyInterface2.P()
 { return "DuplicateInterfaceClass.P()"; }

 public string P()
 { return ((MyInterface2)this).P(); }
}
Julien
  • 1,181
  • 10
  • 31
  • thanks Julien that does give me the idea to use the following in the Main()... Console.WriteLine(((IMyInterface2)test).P()); – user1229895 Mar 02 '12 at 09:04