To implement an interface member, the corresponding member of the implementing class must be public. source: Interfaces (C# Programming Guide)
我知道如果它是私有(private)的,它就可以工作,但我想知道为什么它不能公开?
最佳答案
当显式实现时,默认情况下接口(interface)方法是公共(public)的,这就是您不能使用访问修饰符的原因。
来自 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)
来源:https://msdn.microsoft.com/en-us/library/aa288461%28v=vs.71%29.aspx
附言 隐式和显式实现之间的区别:
interface MyInterface
{
void MyMethod();
}
class A : MyInterface // Implicit implementation
{
public void MyMethod () { ... }
}
class B: MyInterface // Explicit implementation
{
void MyInterface.MyMethod () { ... }
}
关于c# - 实现 IEnumerable<T> 和 IEnumerable.GetEnumerator() 不能公开,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31190025/