Hiding Members via Explicit Interface Implementation in C#

Hiding Members via Explicit Interface Implementation in C#

Explicit Interface Implementation is a complex and rarely used technique that allows a single class to inherit from multiple interfaces that have the same method signature. Explicitly implementing each interface, prepending the interface name to the method, allows the same method signature to exist in the same class. I shall not demonstrate that here, but there are plenty of examples of this online. There is another benefit to explicitly implementing an interface, and that is to hide members from being accessed via the class reference. I’ll create an example of this technique using a factory design pattern and Fruit class and a console application that you can step through via F11. If you create a new C# console application in Visual Studio, then copy and paste all of the below code, you can step through (F11) the code to see how it works.