Inheritance from multiple interfaces with the same method name

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();

You must use explicit interface implementation


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() { }        
}

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");