Explicit Interface Implementation


In the above example function F1( ) of class C3 is providing same implementation to the function F1( )of 11 interface and also the to the function F1 ( ) of 12 interface
But we want to provide different implementations to the function F1 ( ) inherited from 11 interface and to the function F1 ( ) inherited from 12 interface with in the derived class C3. This is known as Explicit Interface Implementation
Example :-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CAInterface2
{
    interface I1
    {
        void F1();
    }
    interface I2
    {
        void F1();

    }
    class C : I1, I2
    {
        void I1.F1()
        {
            Console.WriteLine("This is overriding function of I1 Interface");
        }
        void I2.F1()
        {
            Console.WriteLine("This is overriding function of I2 Interface");
        }
    }
    class CLSInterface2
    {
        static void Main(string[] args)
        {
            C obj1 = new C();
            I1 obj2 = (I1)obj1;
            I2 obj3 = (I2)obj1;
            obj2.F1();
            obj3.F1();
            Console.ReadLine();
        }
    }
}



Output :-



No comments:

Post a Comment