Sunday, 19 August 2018

C#: Explicit interface implementation

Let me explain with an example. Suppose there are two interfaces Interface1, Interface2 with same method signature like ‘void print’.

Program.cs
using System;

interface Interface1
{
    void print();
}

interface Interface2
{
    void print();
}

class MyClass : Interface1, Interface2
{
    public void print()
    {
        Console.WriteLine("Hello World");
    }
}
class Program
{
    static void Main(string[] args)
    {
        MyClass obj = new MyClass();
        obj.print();
    }
}

Output
Hello World

Notify above snippet, ‘MyClass’ implements both the interfaces Interface1, Interface2 and provide implementation for print method. But sometimes you may want to provide different implementations for ‘print’ method of Interface1 and ‘print’ method of Interface2. You can do that by using explicit interface implementation.

You can provide explicit implementation like below.

class MyClass : Interface1, Interface2
{
    void Interface1.print()
    {
        Console.WriteLine("Hello Interface1");
    }

    void Interface2.print()
    {
        Console.WriteLine("Hello Interface2");
    }
}

When a class explicitly implements an interface, the interface method is no longer accessed through class reference variable, it should be accessed through interface reference variable. Access modifiers are not allowed while implementing the interfaces explicitly.

Program.cs

using System;

interface Interface1
{
    void print();
}

interface Interface2
{
    void print();
}

class MyClass : Interface1, Interface2
{
    void Interface1.print()
    {
        Console.WriteLine("Hello Interface1");
    }

    void Interface2.print()
    {
        Console.WriteLine("Hello Interface2");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Interface1 obj1 = new MyClass();
        Interface2 obj2 = new MyClass();

        obj1.print();
        obj2.print();
    }
}

Output
Hello Interface1
Hello Interface2

Providing default implementation
You can also specify default implementation to a method.

class MyClass : Interface1, Interface2
{
    public void print()
    {
        Console.WriteLine("Hello World");
    }

    void Interface1.print()
    {
        Console.WriteLine("Hello Interface1");
    }

    void Interface2.print()
    {
        Console.WriteLine("Hello Interface2");
    }
}

Following statement calls default implementation.
MyClass obj3 = new MyClass();      // Prints Hello World


Program.cs

using System;

interface Interface1
{
    void print();
}

interface Interface2
{
    void print();
}

class MyClass : Interface1, Interface2
{
    public void print()
    {
        Console.WriteLine("Hello World");
    }

    void Interface1.print()
    {
        Console.WriteLine("Hello Interface1");
    }

    void Interface2.print()
    {
        Console.WriteLine("Hello Interface2");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Interface1 obj1 = new MyClass();
        Interface2 obj2 = new MyClass();
        MyClass obj3 = new MyClass();

        obj1.print();
        obj2.print();
        obj3.print();

    }
}

Output
Hello Interface1
Hello Interface2
Hello World


Previous                                                 Next                                                 Home

No comments:

Post a Comment