C# - What is the reason for “Explicitly implemented interface members are always implicitly private ?”

后端 未结 3 1877
执笔经年
执笔经年 2020-12-11 17:42

When i need to implement the interface member explicitly ,it is private.

for example :

 // when explicit implementation it is always private
  void I         


        
3条回答
  •  一生所求
    2020-12-11 18:23

    Because you are saying when my object is an IPointy than you can call the IPointy method draw. Otherwise no.

     public interface IPointy
    {
        void Draw();
    }
    
    public class PointyImplementer : IPointy
    {
        #region IPointy Members
    
        void IPointy.Draw()
        {
            Console.WriteLine("You have drawn");
        }
    
        #endregion
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            IPointy pi = new PointyImplementer();
    
            pi.Draw();
    
            Console.Read();
        }
    }
    

    It is a way for you to mask the interface you are implementing.

提交回复
热议问题