Tuesday, June 26, 2012

Explicit Interface Implementation

Below is an example on how a class can implement multiple interfaces with the same method name. "Method1()" in the below example.  It can done using  explicit interface implementaion.


// Interface 1
interface Interface1// Interface 2

{
void Method1();
}

interface Interface2
{
void Method1();
}
 
// Class implements interfaces

class TestInterface : Interface1, Interface2
{
//Explicit Interface implementation
void Interface1.Method1()
{
Console.WriteLine(" Hello world from Interface 1");
}
void Interface2.Method1()
{
Console.WriteLine(" Hello world from Interface 2");
}
}
  
static void Main(string[] args)
{
Interface1 I1 = new TestInterface();
I1.Method1();
Interface2 I2 = new TestInterface();
I2.Method1();
}

No comments:

Post a Comment