简体   繁体   English

实现IEnumerable <T> 和IEnumerable.GetEnumerator()无法公开,为什么?

[英]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.

相关问题 在包装字典类中实现IEnumerable.GetEnumerator() - Implementing IEnumerable.GetEnumerator() in a wrapper dictionary class 何时调用IEnumerable.GetEnumerator而不是IEnumerable <T> .GetEnumerator? - When does IEnumerable.GetEnumerator get called instead of IEnumerable<T>.GetEnumerator? 如果 GetEnumerator() 的返回类型不同,IEnumerable.GetEnumerator() 的首选实现如何返回 GetEnumerator()? - How can IEnumerable.GetEnumerator()'s go-to implemetation is to return GetEnumerator(), if GetEnumerator() is of diffrent return type? 有关IEnumerable.GetEnumerator()的错误消息 - Error message regarding IEnumerable.GetEnumerator() 动态生成的类,实现IEnumerator <T> GetEnumerator()和IEnumerator IEnumerable.GetEnumerator() - Dynamically generated class that implements IEnumerator<T> GetEnumerator() and IEnumerator IEnumerable.GetEnumerator() 如果我只能定义一个GetEnumerator,为什么还要实现IEnumerable(T)? - Why implement IEnumerable(T) if I can just define ONE GetEnumerator? IEnumerable的 <T> GetEnumerator()执行 - IEnumerable<T> GetEnumerator() exexcution C#:你如何测试IEnumerable.GetEnumerator()方法? - C#: How do you test the IEnumerable.GetEnumerator() method? 为什么要列出<t>声明 GetEnumerator() 和 IEnumerable<t> .GetEnumerator()?</t></t> - Why does List<T> declare GetEnumerator() and IEnumerable<T>.GetEnumerator()? 实现IEnumerable时GetEnumerator()的推荐行为 <T> 和IEnumerator <T> - Recommended behaviour of GetEnumerator() when implementing IEnumerable<T> and IEnumerator<T>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM