实现 IEnumerable<T> 和 IEnumerable.GetEnumerator() 不能是 public,为什么?
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)
我知道如果它是私有的,它就可以工作,但我想了解为什么它不能 public?
当显式实现时,接口方法默认为 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
P.S。
隐式和显式实现之间的区别:
interface MyInterface
{
void MyMethod();
}
class A : MyInterface // Implicit implementation
{
public void MyMethod () { ... }
}
class B: MyInterface // Explicit implementation
{
void MyInterface.MyMethod () { ... }
}
To implement an interface member, the corresponding member of the implementing class must be public. source: Interfaces (C# Programming Guide)
我知道如果它是私有的,它就可以工作,但我想了解为什么它不能 public?
当显式实现时,接口方法默认为 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
P.S。 隐式和显式实现之间的区别:
interface MyInterface
{
void MyMethod();
}
class A : MyInterface // Implicit implementation
{
public void MyMethod () { ... }
}
class B: MyInterface // Explicit implementation
{
void MyInterface.MyMethod () { ... }
}