简体   繁体   English

是否有与STL输出迭代器等效的C#?

[英]Is there a C# equivalent to the STL output iterator?

I'm looking for some type of iterator/enumerator that can write to the backing collection. 我正在寻找可以写入支持集合的某种类型的迭代器/枚举器。 As I understand it c# enumerators are read-only. 据我了解,c#枚举器是只读的。

There's nothing already existing in C# to do such a thing. C#中没有任何东西可以用来做这样的事情。

You're right IEnumerator's Current property is defined as a getter only. 你是对的IEnumerator的Current属性只定义为getter。

You'd need to write a new class and/or interface to support such a thing. 你需要编写一个新的类和/或接口来支持这样的事情。

interface IOutputable<T> {
  IOutputer<T> GetOutputer();
  }

interface IOutputer<T> {

  T Current { set; }

  bool MoveNext();
  void Reset();
  }

AFAIK an output iterator is a way to create a sequence of objects. AFAIK输出迭代器是一种创建对象序列的方法。 There's a myriad of ways to do that in C#. 在C#中有很多方法可以做到这一点。 For example, using a Stack . 例如,使用Stack Instead of doing a C++ style increment/assign operation you'd do a push: 而不是做C ++样式增量/分配操作,你做推动:

var sequence = new Stack<int>();
sequence.Push( 1 );
sequence.Push( 2 );

Unless you have a very specific application for it, there's probably no benefit in trying to emulate output iterators in C#. 除非你有一个非常具体的应用程序,否则尝试在C#中模拟输出迭代器可能没什么好处。

根据您要做的事情, yield return可能会做您正在寻找的。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM