← Back to all articles

Observable.Generate Pop Quiz

What would be the output of the following program;

var xs = new int[]{1,2,3};
Observable.Generate(
    xs.GetEnumerator(),    // initial state
    e => e.MoveNext(),    // break condition
    e => e,            // iterate
    e => e.Current        // result selector
).Subscribe(Console.WriteLine);

The program (correctly) outputs;

1
2
3

What about this program? Would the output be the same? If not why not?

var xs = new List{1,2,3};

Observable.Generate(
    xs.GetEnumerator(),    // initial state
    e => e.MoveNext(),    // break condition
    e => e,                // iterate
    e => e.Current        // result selector
).Subscribe(Console.WriteLine);

Stay tuned.

Comments