When you implement two interfaces with the same method, how do you know which one is being called?

If you have TheMethod() in interfaces I1 and I2 and the following class

 class TheClass : I1, I2 { void TheMethod() } 

If something creates an instance of TheClass , how does it know which interface it uses?

+4
source share
8 answers

If this is how client code uses the class, it doesn't really matter. If he needs to do something with a specific interface, he must declare the interface he needs and assign a class to this, for example.

 I1 i = new TheClass() i.TheMethod(); 

Of course, using the current implementation of TheClass , it doesn't matter if i declared as I1 or I2 , since you only have one implementation.

If you need a separate implementation for each interface, you need to create explicit implementations ...

  void I1.TheMethod() { Console.WriteLine("I1"); } void I2.TheMethod() { Console.WriteLine("I2"); } 

But keep in mind that explicit implementations may not be publicly available. You can only implement one explicitly, and leave it as the default publicly available.

  void I1.TheMethod() { Console.WriteLine("I1"); } public void TheMethod() { Console.WriteLine("Default"); } 

See the msdn article for more details.

+7
source

If both methods are publicly available, then the same method will be called regardless of which interface it was called with. If you need more granularity, you need to use explicit interface implementations:

 class TheClass : I1, I2 { void I1.TheMethod() {} void I2.TheMethod() {} } 

Usage may look like this:

 TheClass theClass = new TheClass(); I1 i1 = theClass; I2 i2 = theClass; i1.TheMethod(); // (calls the first one) i2.TheMethod(); // (calls the other one) 

Keep in mind that if you make both implementations explicit, you can no longer call TheMethod on variables declared as TheClass :

 theClass.TheMethod(); // This would fail since the method can only be called on the interface 

Of course, if you want, you can only make one of the implementations explicit and keep the other public, in this case calls from TheClass will call the public version.

+5
source

You are not using the interface in the sense of invoking it. An interface is simply a contract defining which methods you can name essentially. You always call an implementation.

+3
source

If you create an instance of the class, then you are not using any of the interfaces. If you pass a link to any of the interfaces, then you use this interface. Example:

 TheClass c = new TheClass(); c.TheMethod(); // using the class I1 i = new TheClass(); i.TheMethod(); // using the I1 interface 

As your class is declared, both interfaces will use the same method. You can also specify methods for the class and individual interfaces:

 class TheClass : I1, I2 { void TheMethod() {} // used by the class void I1.TheMethod() {} // used by the I1 interface void I2.TheMethod() {} // used by the I2 interface } 

If you specify only methods for interfaces, you cannot reach this method unless you first pass a link to the interface:

 class TheClass : I1, I2 { void I1.TheMethod() {} // used by the I1 interface void I2.TheMethod() {} // used by the I2 interface } 

If you specify separate methods only for some interfaces, other interfaces will use the same implementation as the class:

 class TheClass : I1, I2 { void TheMethod() {} // used by the class and the I1 interface void I2.TheMethod() {} // used by the I2 interface } 
+2
source

As long as the method signatures are the same, it is logical for the method to implement methods of two or more interfaces.

There is no way to find out "through which interface" the method is called - there is no such thing (and it does not matter).

+1
source

The real question is: "Who needs the interface that it uses?" Indeed, this is not a โ€œuse" of the interface at all. It uses an implementation to execute an interface.

0
source

It does not matter. Casting to any of the interfaces will result in a method call, but since the interface cannot contain code, you cannot inherit its behavior.

0
source
 public interface IA { void Sum(); } public interface IB { void Sum(); } public class SumC : IA, IB { void IA.Sum() { Console.WriteLine("IA"); } void IB.Sum() { Console.WriteLine("IB"); } public void Sum() { Console.WriteLine("Default"); } } public class MainClass { static void Main() { IA objIA = new SumC(); IB objIB = new SumC(); SumC objC = new SumC(); objIA.Sum(); objIB.Sum(); objC.Sum(); Console.ReadLine(); } } 
0
source

All Articles