Question

I have two Interfaces A,B (residing in different components). Both declare a method with the same signature ( MyMethod ). The two interfaces are inherited by a third Interface (C).

The method which is declared in the first two interfaces (A,B) is intended to return always the same values (for A and B) and thus, I don’t want to implement the interfaces explicitly when deriving from C.
I achieve this with declaring the method also in the third interface, while using the new-keyword.

public interface A {
   MyType MyMethod();
}
public interface B {
   MyType MyMethod();
}

public interface C : A,B{
   new MyType MyMethod();
}

public class ImplementingClass : C{
   public MyType MyMethod(){
       // do somethin
       // return something
   }
}

Are there problems to be expected with this, or is this bad style?

Update
Sorry, my initial question has not shown the full story. The Problem arises, when I try to call MyMethod on a interface-reference of C. The Compiler will not compile this.

 C aReferenceToC=new CImplementingClass();
 aReferenceToC.MyMethod(); // <<< Here the compiler will throw an exception

Full example

C myCImplementationAsAnInterfaceReference = new MyCImplementation(); 
myCImplementationAsAnInterfaceReference.MyMethod(); // This does not compile without declaring MyMethod in C with the new-Keyword

MyCImplementation myCImplementationReference= new MyCImplementation(); 
myCImplementationReference.MyMethod(); // This however will always compile and run

public interface A {
        int MyMethod();
}
public interface B {
        int MyMethod();
}

public interface C : A, B {

}

public class MyCImplementation : C {

        public int MyMethod() {
            return 1;
        }
}
Was it helpful?

Solution

Doing what you do, does not in any way stop people from giving different implementations of A.MyMethod, B.MyMethod, and C.MyMethod.

  class TestABC : C
  {
    MyType C.MyMethod()
    {
      // 1
      return null;
    }

    MyType A.MyMethod()
    {
      // 2
      return null;
    }

    MyType B.MyMethod()
    {
      // 3
      return null;
    }
  }

The new keyword does not in anyway remove the "hidden" method. It just tells the compiler to tolerate the fact that the type has now two identical methods with the same signature, one inherited from a base type, and one declared by the current type.

Edit: OK, given the development of your question, here's what I think your problem really was (this wasn't clear to me initially):

You had this design:

public interface A {
   MyType MyMethod();
}
public interface B {
   MyType MyMethod();
}

public interface C : A,B{
}

Your problem was that this code didn't compile:

C myInstance = CreateAnInstanceOfSomeClassImplementingC();
myInstance.MyMethod();  //  does not compile, ambiguous

Your question is if it's an OK solution to get rid of the compiler error error CS0121: The call is ambiguous between the following methods or properties: [...] by introducing the new member in C.

To me it seems ugly. But i cannot come up with another solution (when you informed me in the comments that C could not be a class). There's no way for an interface to dictate that two methods inherited must unite.

OTHER TIPS

Whether using new keyword or not does not really change anything here. the behavior is just the same.

Testing testing = new Testing();
testing.MyMethod(); // calls Testing.MyMethod

AA testingA = new Testing();
testingA.MyMethod(); // calls AA.MyMethod


 public interface A
{
    int MyMethod();
}

public class AA : A
{

    public int MyMethod()
    {
        return 11;
    }
}

public interface B
{
    int MyMethod();
}

public interface C : A, B
{
    int MyMethod();
}

public class Testing : AA,C
{
    public int MyMethod()
    {
        return 10;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top