#456 – Explicitly Implemented Interface Members Are Automatically Private

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).

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

One Response to #456 – Explicitly Implemented Interface Members Are Automatically Private

  1. kurkevan says:

    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!

Leave a comment