menu

Wednesday, August 28, 2013

Implement Two Interfaces With Same Method Name in C#

using System;
interface I1
{
    void Fun();
}
interface I2
{
    void Fun();
    void Fun1();
}
public class C : I1, I2
{
    public void Fun()  
    {
        Console.WriteLine("I1::Fun()");
    }
    /* This function will be implemented as private
     * because one more implementation with same name
     * is already from another base type(I1)*/
    void I2.Fun()
    {
        Console.WriteLine("I2::Fun()");
    }
    public void Fun1()
    {
        Console.WriteLine("I2::Fun1()");
    }
}
public class A
{
    public static void Main()
    {
        I1 o1 = new C();
        o1.Fun();            // Of I1

        I2 o2 = new C();
        o2.Fun();            // Of I2
        o2.Fun1();           // Of I2
       
        C o = new C();
        o.Fun();             // Of I1
        o.Fun1();            // Of I1

        Console.ReadKey();
    }

}

No comments:

Post a Comment