简体   繁体   中英

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.

There's nothing already existing in C# to do such a thing.

You're right IEnumerator's Current property is defined as a getter only.

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. There's a myriad of ways to do that in C#. For example, using a Stack . Instead of doing a C++ style increment/assign operation you'd do a push:

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#.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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