[英]implementing IEnumerable<T> and IEnumerable.GetEnumerator() can not be public, why?
To implement an interface member, the corresponding member of the implementing class must be public.
要实现接口成员,实现类的相应成员必须是公共的。 source: Interfaces (C# Programming Guide)
来源: 接口(C#编程指南)
I know it works if its private, but i would like to understand why it can not be public ? 我知道它是私有的,但我想了解为什么它不能公开?
When implemented explicitly interface methods are public by default and that's why you cannot use access modifiers. 明确实现后,默认情况下接口方法是公共的,这就是为什么您不能使用访问修饰符的原因。
A quote from msdn.com : 来自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 来源: https : //msdn.microsoft.com/en-us/library/aa288461%28v=vs.71%29.aspx
PS Difference between implicit and explicit implementation : PS隐式和显式实现之间的区别:
interface MyInterface
{
void MyMethod();
}
class A : MyInterface // Implicit implementation
{
public void MyMethod () { ... }
}
class B: MyInterface // Explicit implementation
{
void MyInterface.MyMethod () { ... }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.