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

Advertisements