简体   繁体   English

为什么IEnumerable <T> 有IEnumerator时需要 <T> ?

[英]Why is IEnumerable<T> necessary when there is IEnumerator<T>?

Disclaimer: I understand the difference between IEnumerable<T> and IEnumerator<T> and how to use both. 免责声明:我理解IEnumerable<T>IEnumerator<T>之间的区别以及如何使用它们。 This is not a duplicate of this or this . 这与不重复。

This is more of a design question - since IEnumerator<T> already encapsulates all the necessary information ( .Current , .MoveNext() ) about something that can be enumerated, then what's the point of introducing a type ( IEnumerable<T> ) whose sole purpose is to return an instance of the former? 这更像是一个设计问题 - 因为IEnumerator<T>已经封装了可以枚举的所有必要信息( .Current.MoveNext() ),那么引入一个类型( IEnumerable<T> )的重点是什么呢?唯一的目的是返回前者的实例?

To be specific: 再具体一点:

  1. Why can't foreach be designed to iterate directly through an IEnumerator<T> , like so: 为什么不能将foreach设计为直接通过IEnumerator<T>进行迭代,如下所示:

     // foreach (var e in anEnumerator) { //... } while (anEnumerator.MoveNext()) { doSomething(anEnumerator.Current); } 
  2. Why can't Linq be built based directly off of IEnumerator<T> ? 为什么Linq不能直接基于IEnumerator<T>构建?

The two interfaces each represent very different concepts. 这两个接口各自代表非常不同的概念。 IEnumerable<T> is something that "allows enumeration", where IEnumerator<T> is the representation of the enumeration itself. IEnumerable<T>是“允许枚举”的东西,其中IEnumerator<T>是枚举本身的表示。

If you were to merge these together, it would be impossible to enumerate a single collection more than once at the same time (without some other mechanism in place). 如果要将这些合并在一起,则不可能同时枚举单个集合(没有其他一些机制)。 For example, two threads doing a foreach over an array would no longer work, where that is perfectly acceptable in the current design. 例如,在数组上执行foreach两个线程将不再起作用,这在当前设计中是完全可接受的。

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

相关问题 为什么在实现IEnumerable时需要实现IEnumerator - Why is it necessary to implement IEnumerator when Implementing IEnumerable 以下为什么不工作? (IEnumerable / IEnumerator) - Why doesn't the following work? (IEnumerable/ IEnumerator) 实现IEnumerable时GetEnumerator()的推荐行为 <T> 和IEnumerator <T> - Recommended behaviour of GetEnumerator() when implementing IEnumerable<T> and IEnumerator<T> foreach是否更喜欢IEnumerable <T> 或IEnumerator <T> - Does foreach prefer IEnumerable<T> or IEnumerator<T> IEnumerator vs IEnumerator <T> - IEnumerator vs IEnumerator<T> 为什么Linq扩展方法不位于IEnumerator而不是IEnumerable? - Why don't the Linq extension methods sit on IEnumerator rather than IEnumerable? 返回IEnumerable的迭代器方法之间有什么区别吗? <T> 和IEnumerator <T> ? - Is there any difference between iterator methods returning IEnumerable<T> and IEnumerator<T>? 将IEnumerable <T>转换为IEnumerator <T>的做法是什么 - What does casting an IEnumerable<T> to IEnumerator<T> do IEnumerator的 <T> / IEnumerable <T> 只暂时存储位置? - IEnumerator<T> / IEnumerable<T> with position stored only temporarily? Rhino mocks:当存在IEnumerable时,IEnumerator.MoveNext()问题 <T> 接口 - Rhino mocks: Issue with IEnumerator.MoveNext() when stubbing an IEnumerable<T> interface
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM