I have a question regarding Interface. There are 2 interface both contain the same Method Test().
Now I’m inheriting both the interface in Sample class.I want to know which Interface’s method will be called?
My code sample is below:
interface IA
{
void Test();
}
interface IB
{
void Test();
}
class Sample: IA, IB
{
public void Test()
{
Console.WriteLine("Which interface will be implemented IA or IB???!");
}
}
class Program
{
public static void Main(string[] args)
{
Sample t = new Sample();
t.Test();//Which Interface's Method will called.
Console.ReadLine();
}
}
Thanks
Vijendra Singh
The result will be the same for both. If you want different behaviour per interface, you have to explicitly implement them:
If you want default behaviour, create a method with the same signature (thus an implicit interface implementation) and add code for that case. Usually, you just want the explicit implementation though.