c# - 实现 IEnumerable<T> 和 IEnumerable.GetEnumerator() 不能公开,为什么?

标签 c# .net enumerator ienumerator

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

我知道如果它是私有(private)的,它就可以工作,但我想知道为什么它不能公开?

must be private interface member implementation

最佳答案

当显式实现时,默认情况下接口(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/

相关文章:

.net - 实体数据模型无法遵循 web.config 中的 configSource 属性

regex - 使用正则表达式等标准生成字符串

c# - Parallel.Foreach 会阻塞直到完成吗?

c# - 通过 TcpClient 的网络流写入大数据的最佳方式

c# - 基于 24 小时制的小时间隔

c# - 一个类型的多个索引属性?

scala - 如何编写被枚举者沿不同边界对枚举器进行分块

python - 迭代器和枚举器对象之间的区别

c# - Kendo TabStrip Ajax (LoadContentForm) 内的 Kendo Grid Ajax 不显示内容

c# - 使每个 block 内的更改持久化