简体   繁体   English

实现的方法的返回类型接口,Ienumerator

[英]Return type interface for the method implemented, Ienumerator

the GetEnumerator return type is the interface itself ie Ienumerable, wondering what it means when an interface can be a method's return type; GetEnumerator返回类型是接口本身,即Ienumerable,想知道什么时候接口可以是方法的返回类型? specially when its defined in that interface. 特别是当它在该接口中定义时。

Ienumerator enum = arrayList.GetEnumerator();

If I understood you correctly. 如果我理解正确的话。 ArrayList doesn't implement IEnumerator , rather IEnumerable . ArrayList没有实现IEnumerator ,而是IEnumerable

public class ArrayList : IList, ICollection, IEnumerable, ICloneable 

IEnumerable IEnumerable的

public interface IEnumerable
{
    IEnumerator GetEnumerator();
}

Edit: 编辑:

IEnumerator has one simple responsibility. IEnumerator的职责很简单。

Supports a simple iteration over a non-generic collection. 支持对非泛型集合的简单迭代。

ArrayList's responsibility is different ArrayList的责任不同

Implements the IList interface using an array whose size is dynamically increased as required. 使用数组实现IList接口,该数组的大小根据需要动态增加。

May be this is required for maintaining of SRP. 可能是维护SRP所必需的。

ArrayList's implementation of GetEnumerator() probably returns implementation of IEnumerable which is aware of implementation details of ArrayList. ArrayList的GetEnumerator()实现可能返回IEnumerable的实现,该实现知道ArrayList的实现细节。 But concrete type is hidden from the client behind the IEnumerator interface. 但是具体类型在IEnumerator接口后面的客户端中是隐藏的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使接口方法返回实现它的类的类型的对象 - Make a interface method return an object of type of the class which implemented it C#函数返回类型的已实现接口 - C# function to return the implemented interface of a type IEnumerator的返回类型GetEnumerator()? - return type of public IEnumerator GetEnumerator()? 接口方法返回类型是实现接口的类 - Interface method return type to be class that implements the interface 为什么接口 IEnumerable 返回 IEnumerator GetEnumemrator()? 为什么不只实现接口 IEnumerator? - Why does interface IEnumerable return IEnumerator GetEnumemrator()? Why not just implement interface IEnumerator? 如何使用Type.InvokeMember来调用显式实现的接口方法? - How to use Type.InvokeMember to call an explicitly implemented interface method? 反思说接口方法在实现类型中是虚拟的,不是吗? - Reflection says that interface method are virtual in the implemented type, when they aren't? 在协变类型的接口中使用具有协变类型的接口作为方法的返回类型 - Using interface with contravariant type as a return type of a method in an interface with covariant type 将对象传递给具有接口返回类型的方法 - Pass an object to a method with Interface return type c#从方法返回接口类型 - c# return interface type from method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM