implementing IEnumerable and IEnumerable.GetEnu

2019-02-25 13:44发布

To implement an interface member, the corresponding member of the implementing class must be public. source: Interfaces (C# Programming Guide)

I know it works if its private, but i would like to understand why it can not be public ?

must be private interface member implementation

1条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-02-25 14:14

When implemented explicitly interface methods are public by default and that's why you cannot use access modifiers.

A quote from msdn.com :

When a member is explicitly implemented, it cannot be accessed through a class instance, but only through an instance of the interface (which is public by default)

source : https://msdn.microsoft.com/en-us/library/aa288461%28v=vs.71%29.aspx

P.S. Difference between implicit and explicit implementation :

interface MyInterface
{
   void MyMethod();
}

class A : MyInterface  // Implicit implementation
{
   public void MyMethod () { ... }
}

class B: MyInterface   // Explicit implementation
{
   void MyInterface.MyMethod () { ... }
}
查看更多
登录 后发表回答