5

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.

dario_ramos
  • 7,118
  • 9
  • 61
  • 108
ccarton
  • 3,556
  • 16
  • 17

3 Answers3

3

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

Marnix van Valen
  • 13,265
  • 4
  • 47
  • 74
  • This should be the accepted answer. MerickOWA's approach may work, but it's not the C# way of doing things. – dan04 Dec 16 '11 at 15:00
2

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();
  }
MerickOWA
  • 7,453
  • 1
  • 35
  • 56
0

Depending on what you're trying to do, a yield return might do what you're looking for as well.

Bryan Boettcher
  • 4,412
  • 1
  • 28
  • 49