C# allows you to explicitly allow some methods to only be called when you cast to the interface you are coding to.
interface I1 {
void mymethod();
}
interface I2 {
void mymethod();
}
public class SubClass : I1, I2{
void I1.mymethod(){
Console.WriteLine("i1 mymethod");
}
void I2.mymethod(){
Console.WriteLine("i2 mymethod");
}
}
In the main method we have the following code
SubClass subClass = new();
// output: i1 mymethod
((I1)subClass).mymethod();
// output: i2 mymethod
((I2)subClass).mymethod();
// compile time error
subClass.mymethod();
Use Cases