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 we can specify that which method of which interface is implemented?
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");
}
}
Note that the functions are private on the class, so you have to assign an object of that class to a variable defined as that particular interface:
var dual = new Dual();
ITest test = dual;
test.Test();
ITest2 test2 = dual;
test2.Test();