Tuesday, February 15, 2011

.NET Interview questions for Interfaces:- If A class inherits from multiple interfaces and the interfaces have same method names. How can we provide different implementation?.

.NET Interview questions for Interfaces:- If A class inherits from multiple interfaces and the interfaces have same method names. How can we provide different implementation?.


Answer:
This is again one those typical .NET Interview questions which move around interfaces to confuse the developer.

For providing differentimplementation you can qualify the method names with interface names as as shown below. A reference to I1 will display "This is I1" and reference to I2 will display "This is I2". Below is the code snippet for the same.

interface I1
{
void MyMethod();
}
interface I2
{
void MyMethod();
}
class Sample : I1, I2
{
public void I1.MyMethod()
{
Console.WriteLine("This is I1");
}
public void I2.MyMethod()
{
Console.WriteLine("This is I2");
} 
}

50 .NET Interview questions and answers

No comments: