Here is a sample code to retrieve data from a database using the yield keyword that I found in a few place while googling around :
public IEnumerable
Judging from this technical explanation, your code will not work as expected, but abort on the second item, because the connection was already closed when returning the first item.
@Joel Gauvreau : Yes, I should have read on. Part 3 of this series explains that the compiler adds special handling for finally blocks to trigger only at the real end.
From the simple test I have tried, aku is right, dispose is called as soon as the foreach block exit.
@David : However call stack is kept between call, so the connection would not be closed because on the next call we would return to the next instruction after the yield, which is the while block.
My understanding is that when the iterator is disposed, the connection would also be disposed with it. I also think that the Connection.Close would not be needed because it would be taken care of when the object is disposed because of the using clause.
Here is a simple program I tried to test the behavior...
class Program
{
static void Main(string[] args)
{
foreach (int v in getValues())
{
Console.WriteLine(v);
}
Console.ReadKey();
foreach (int v in getValues())
{
Console.WriteLine(v);
break;
}
Console.ReadKey();
}
public static IEnumerable<int> getValues()
{
using (TestDisposable t = new TestDisposable())
{
for(int i = 0; i<10; i++)
yield return t.GetValue();
}
}
}
public class TestDisposable : IDisposable
{
private int value;
public void Dispose()
{
Console.WriteLine("Disposed");
}
public int GetValue()
{
value += 1;
return value;
}
}
Connection will be closed automatically since you're using it inside "using" block.
The Iterator that the compiler synthesises implements IDisposable
, which foreach
calls when the foreach
loop is exited.
The Iterator's Dispose()
method will clean up the using
statements on early exit.
As long as you use the iterator in a foreach
loop, using()
block, or call the Dispose()
method in some other way, the cleanup of the Iterator will happen.