If we have a class that inherits from multiple interfaces, and the interfaces have methods with the same name, how can we implement these methods in my class? How can we specify which method of which interface is implemented?
9 Answers
By implementing the interface explicitly, like this:
public interface ITest {
void Test();
}
public interface ITest2 {
void Test();
}
public class Dual : ITest, ITest2
{
void ITest.Test() {
Console.WriteLine("ITest.Test");
}
void ITest2.Test() {
Console.WriteLine("ITest2.Test");
}
}
When using explicit interface implementations, the functions are not public on the class. Therefore in order to access these functions, you have to first cast the object to the interface type, or assign it to a variable declared of the interface type.
var dual = new Dual();
// Call the ITest.Test() function by first assigning to an explicitly typed variable
ITest test = dual;
test.Test();
// Call the ITest2.Test() function by using a type cast.
((ITest2)dual).Test();

- 12,206
- 8
- 54
- 70
-
what if Test() is declared public in both the interfaces and the Dual class? – R.S.K Nov 17 '14 at 18:31
-
8Methods/properties on an interface are always public - that is the entire point of an interface. But when you use an _explicit interface implementation_, the function will always be private on the class. That is why you need the technique to cast the class to the specific interface that you need. When you do implement two interfaces that define methods with identical signature, you cannot avoid this. – Pete Nov 17 '14 at 20:49
-
Do note another subtle "feature" of this is that each method can have a different return type even if they have the same method signature. If you don't use explicit interface declaration on all or all but one of the methods, this isn't normally allowed. – NightOwl888 Jan 18 '18 at 00:11
-
@R.S.K If Test() is declared in the Dual class, it will be used by both interfaces as the implementation. You won't be able to have the other *two* explicit implementations along with that. – Ε Г И І И О Aug 27 '18 at 04:52
You can implement one or both of those interfaces explicitly.
Say that you have these interfaces:
public interface IFoo1
{
void DoStuff();
}
public interface IFoo2
{
void DoStuff();
}
You can implement both like this:
public class Foo : IFoo1, IFoo2
{
void IFoo1.DoStuff() { }
void IFoo2.DoStuff() { }
}

- 225,310
- 48
- 427
- 736
You can implement one interface Explicitly and another implecitely.
public interface ITest {
void Test();
}
public interface ITest2 {
void Test();
}
public class Dual : ITest, ITest2
{
public void Test() {
Console.WriteLine("ITest.Test");
}
void ITest2.Test() {
Console.WriteLine("ITest2.Test");
}
}
ITest.Test
will be the default implementation.
Dual dual = new Dual();
dual.Test();
((ITest2)dual).Test();
Output:
Console.WriteLine("ITest.Test");
Console.WriteLine("ITest2.Test");

- 1,882
- 5
- 28
- 53
Sometimes you may even need to do:
public class Foo : IFoo1, IFoo2
{
public void IFoo1.DoStuff() { }
public void IFoo2.DoStuff()
{
((IFoo1)this).DoStuff();
}
}

- 39
- 1
-
Versus creating a temporary of the correct interface type? No thanks. `readonly IFoo1 this1 = this; this1.DoStuff();` – Thomas Eding Dec 28 '11 at 08:13
-
4Inheritable classes which include code in explicit interface implementations often cause trouble for derived classes. I would suggest as an alternate pattern having a `protected` method with the code for both interface members, and having both interface members chain to that. Casting `this` to an interface type for the purpose of using a member thereof is generally a significant code smell. Better to have a `protected` method which implements the member in question and chain to that. – supercat Feb 28 '14 at 18:02
public class ImplementingClass : AClass1, IClass1, IClass2
{
public override string Method()
{
return "AClass1";
}
string IClass1.Method()
{
return "IClass1";
}
string IClass2.Method()
{
return "IClass2";
}
}
So when calling from different class you will have to type cast the object into required Interface or Abstract class.
ImplementingClass implementingClass = new ImplementingClass();
((AClass1)implementingClass).Method();

- 31
- 5
public interface IDemo1
{
void Test();
}
public interface IDemo2
{
void Test();
}
public class clsDerived:IDemo1,IDemo2
{
void IDemo1.Test()
{
Console.WriteLine("IDemo1 Test is fine");
}
void IDemo2.Test()
{
Console.WriteLine("IDemo2 Test is fine");
}
}
public void get_methodes()
{
IDemo1 obj1 = new clsDerived();
IDemo2 obj2 = new clsDerived();
obj1.Test();//Methode of 1st Interface
obj2.Test();//Methode of 2st Interface
}

- 239
- 2
- 3
Answer is "By using explicit Interface implementation"
Take one example:
using System;
interface A
{
void Hello();
}
interface B
{
void Hello();
}
class Test : A, B
{
void A.Hello()
{
Console.WriteLine("Hello to all-A");
}
void B.Hello()
{
Console.WriteLine("Hello to all-B");
}
}
public class interfacetest
{
public static void Main()
{
A Obj1 = new Test();
Obj1.Hello();
B Obj2 = new Test();
Obj2.Hello();
}
}
Output:
Hello to all-A
Hello to all-B

- 1,955
- 5
- 30
- 50

- 538
- 6
- 7
Dual dual = new Dual();
(dual as ITest).Test();
(dual as ITest2).Test();
you can use that for your code

- 57
- 6