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