CodingSup3rnatur@l-360, on 07 July 2012 - 11:59 PM, said:
I don't see a problem with using yield return ... inside a using statement. The resource being 'used' will get disposed when all items have been yielded, or when Dispose() gets called on the IEnumerator<T> object facilitating the iteration.
I don't understand how or when (if it does) when the Iterator part gets disposed off, because in my example I don't yield all of the possible chars of the streams. I just take the first one, which suggests (to me) the Iterator state-machine is still alive somewhere.
using (var e=TT().GetEnumerator())
{
e.MoveNext();
Console.WriteLine(e.Current);
} // Does this Dispose.
// snipped
static System.Collections.Generic.IEnumerable<Char> TT()
{
using (var sr = new System.IO.StreamReader("TextFile1.txt"))
{
while (!sr.EndOfStream)
{
yield return (char) sr.Read(); // Execution of this is paused here
}
} // Cause this disposal to happen.
}
This post has been edited by AdamSpeight2008: 08 July 2012 - 03:48 PM

New Topic/Question
Reply





MultiQuote



|