0

I have this code:

public IEnumerable<ImportRecord> Parse()
{
    var engine = new FileHelperAsyncEngine<Record>();

    using (StreamReader reader = new StreamReader(this.stream))
    using (engine.BeginReadStream(reader))
    {
        foreach (var record in engine)
        {
            yield return record.Convert(this.correlationId);
        }
    }
}

A stream is opened and the function starts yielding results.

My understanding is the enumerable state machine is having to hold the state of the opened stream causing some sort of overhead?

Could there be a situation where the streams aren't disposed, say if you did this: Parse().Skip(10).Take(1); where you aren't enumerating the entire sequence? My guess is no, I would assume the state machine would do something, I'm unsure as to what that something is if someone could elaborate.

Callum Linington
  • 14,213
  • 12
  • 75
  • 154
  • [Possible duplicate](http://stackoverflow.com/a/1539180/1260204) – Igor Mar 18 '16 at 13:41
  • There is a small overhead where the streamreader keeps track of the position of the reader (the byte count) along with boolean's indicating if the reader is at the beginning or end of the file. The major overhead is a temporary buffer (that is adjustable) which holds the current data. For a stream to be disposed both ends of the stream must release the object. it is possible that one end may not release resources and the stream may not get disposed. – jdweng Mar 18 '16 at 13:44

0 Answers0