#456 – Explicitly Implemented Interface Members Are Automatically Private
November 16, 2011 1 Comment
When you explicitly implement an interface member in a class, you make the member accessible only through objects whose type is the interface and not through objects whose type is the class.
public class Cow : IMoo { void IMoo.Moo() { Console.WriteLine(string.Format("{0} says Moo", Name)); } // rest of class here
Cow bessie = new Cow("Bessie", 5); // bessie.Moo(); // Compile-time error IMoo viaMoo = bessie; viaMoo.Moo(); // OK
Because explicitly implemented interface members are not accessible via normal class instances, all explicitly implemented members are implicitly private. These members cannot include an access modifier (either private or public).
How can an explicitly implemented method be “implicitly private” if it is accessible from outside the class? You yourself wrote elsewhere that they are implicitly *public*: https://csharp.2000things.com/2013/03/13/799-interface-members-are-implicitly-public/
p.s. – Thanks for the wonderful site!