Explicit interface implementation
A big part of endjin's apprenticeship is the structured training each apprentice has to undertake. Time is set aside during the working day to do this training - we aren't obliged to complete our training outside of our contracted hours, unlike what I've heard from some of my friends who work in certain financial institutions...
Each apprentice has a training backlog for them to make their way through during the course of their apprenticeship. This consists of numerous items in numerous formats. For example, one item could be the reading of a technical book and the next a Pluralsight course. Training is then put into practice through project work, IP development, blogging, certifications, and so on.
One of the current items on my training backlog is to read through Ian's Programming C# 5.0. This is a comprehensive book on C#, and I've got no choice but to take my time going through it. I'm learning something new on every page, and things I don't understand, I put into practice. Explicit interface implementation, something that Ian mentions in the Interface type section of his book, is something I wanted to use myself.
From what I understand, there are two main use-cases for explicit implementation of an interface, the use of either being relatively uncommon in real-life:
- To hide members of the interface in a class which inherits from that interface, usually since the code using the class only needs to see a select few of those members, or because you don't want the members of that interface to be visible on the public API.
- To work around the scenario when a class is inheriting from two interfaces which share a member of the same name.
Case 1
Suppose I have an interface like this, called IHaveLotsOfMethods
:
Now let's suppose I have a class, called MySimpleClass
, that only needs to have two of the methods of the interface visible publicly. I could do this:
If I now switch to my program, instantiate a variable called simpleClass
of type MySimpleClass
, and access its methods, I obviously see this:
But I know that my class only implements Method1
and Method2
, so the rest of the methods are just noise. How do we hide them? Simple. We use explicit implementation:
Now, this is what I see in my program:
Which is much nicer. If, for some reason, I decide I want to access the un-implemented methods, I can do that by creating an expression referring to the simpleClass
object, using the interface's type:
Case 2
Here are two interfaces which have the same members:
I know that the properties are meant to represent the same thing, but that the method is subtly different in each case. Here's my class which implements both interfaces:
So I have used explicit implementation to access the ChangeName
method of each interface. In my program, again, I only see the public members of class:
So, to access the ChangeName
methods, I reference the person
object using expressions of the separate interfaces' type, like so:
Which allows me to run the next bit of pointless code, and have fun in the process of working out how many times the string "Ed" will appear:
(It's six.)
Thanks for reading!