Explicit interface implementation

As they say: you live and learn. Some time ago I wrote about why I don’t like VB.NET, where I was complaining about strange VB.NET behaviour with implementing interfaces. And recently I found out that in C# there is also something called Explicit Interface Implementation.

So if we take example from my previous post:

interface IDoNothing
    {
        void Method();
    }

    class FirstClass : IDoNothing
    {
        public void Method()
        {
            Console.WriteLine("FirstClass.Method");
        }

        void IDoNothing.Method()
        {
            Console.WriteLine("FirstClass.IDoNothing.Method");
        }
    }
    class PrivateClass : IDoNothing
    {
        public void Method()
        {
            Console.WriteLine("PrivateClassClass.Method");
        }
    }

And then we can call it:

    public class Program
    {
        public static void Main()
        {
            Console.WriteLine("As Interface:");
            IDoNothing asInterface = new FirstClass();
            //Outputs: FirstClass.IDoNothing.Method
            asInterface.Method();

            Console.WriteLine("As Class:");
            FirstClass asClass = new FirstClass();
            //Outputs: FirstClass.Method
            asClass.Method();
            //Outputs: FirstClass.ThisIsMethod
            ((IDoNothing)asClass).Method();

            Console.WriteLine("As Private Class:");
            IDoNothing asPrivateClass = new PrivateClass();
            //Outputs: PrivatClass.Method
            asPrivateClass.Method();
        }
    }

It is quite similar to VB.NET, but for me it much more clearer and there are less possibilities for create obfuscated code (like defining private method which is actually public).

This feature could be very useful when implementing two interfaces with the same method name (we can avoid name conflicts):
First, we define two interfaces with the same method:

interface IColor
{
    string GetName();
}
interface IShape
{
    string GetName();
}

Then we can define class implementing these interfaces:

class Figure : IColor, IShape
{
    string name;
    string color;
    string shape;

    public Figure(string name, string color, string shape)
    {
        this.name = name;
        this.color = color;
        this.shape = shape;
    }

    public string GetName()
    {
        return name;
    }

    string IColor.GetName()
    {
        return color;
    }

    string IShape.GetName()
    {
        return shape;
    }
}

And then we can use it:

public class Program
{
    public static void Main()
    {
        Figure fig = new Figure("John", "Red", "Square");
        Console.WriteLine(fig.GetName()); //John
        IColor color = (IColor)fig;
        Console.WriteLine(color.GetName()); //Red
        //I'm kind of supprised that below line is working in VS2013.
        IShape shape = (IShape)color;
        Console.WriteLine(shape.GetName()); //Square
    }
}

Dodaj komentarz

Twój adres email nie zostanie opublikowany. Pola, których wypełnienie jest wymagane, są oznaczone symbolem *