Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call an explicitly implemented interface-method on the base class

I have a situation, where two classes (one deriving from the other) both implement the same interface explicitly:

interface I {   int M(); }  class A : I {   int I.M() { return 1; } }  class B : A, I {   int I.M() { return 2; } } 

From the derived class' implementation of I.M(), I'd like to call the implementation of the base class, but I don't see how to do it. What I tried so far is this (in class B):

int I.M() { return (base as I).M() + 2; } // this gives a compile-time error //error CS0175: Use of keyword 'base' is not valid in this context  int I.M() { return ((this as A) as I).M() + 2; } // this results in an endless loop, since it calls B's implementation 

Is there a way to do this, without having to implement another (non interface-explicit) helper method?


Update:

I know it's possible with a "helper" method which can be called by the derived class, e.g:

class A : I {     int I.M() { return M2(); }     protected int M2 { return 1; } } 

I can also change it to implement the interface non-explicitly. But I was just wondering if it's possible without any of these workarounds.

like image 875
M4N Avatar asked May 12 '11 09:05

M4N


People also ask

How do you call an explicit interface?

The following is an example of how to call "Explicit Interface Method" in the same class using class methods. Output: Class1 Display Method. Iinterface_1 Method Explicit interface implementation. Iinterface_1 Method Implicit interface implementation.

Can a base class implement an interface?

A base class can also implement interface members by using virtual members. In that case, a derived class can change the interface behavior by overriding the virtual members.

How do you implement an interface and call its methods?

In order to call an interface method from a java program, the program must instantiate the interface implementation program. A method can then be called using the implementation object.

How do you call an interface method in C#?

In order to call the methods using interface reference(here r is interface reference), you have to assign to class object to it. Like if you are assigning Person1's object obj1 to r i.e. r = obj1; then you call the Speed() and Distance() methods that are implemented by the Person1 class.


1 Answers

Unfortunately, it isn't possible.
Not even with a helper method. The helper method has the same problems as your second attempt: this is of type B, even in the base class and will call the implementation of M in B:

interface I {   int M(); } class A : I {   int I.M() { return 1; }   protected int CallM() { return (this as I).M(); } } class B : A, I {   int I.M() { return CallM(); } } 

The only workaround would be a helper method in A that is used in A's implementation of M:

interface I {   int M(); } class A : I {   int I.M() { return CallM(); }   protected int CallM() { return 1; } } class B : A, I {   int I.M() { return CallM(); } } 

But you would need to provide a method like this also for B if there will be a class C : B, I...

like image 95
Daniel Hilgarth Avatar answered Sep 20 '22 22:09

Daniel Hilgarth