Skip to content

Instantly share code, notes, and snippets.

@ruxo
Last active December 29, 2022 12:12
Show Gist options
  • Select an option

  • Save ruxo/9a36a0be4c4eaed857f602dd7db94d26 to your computer and use it in GitHub Desktop.

Select an option

Save ruxo/9a36a0be4c4eaed857f602dd7db94d26 to your computer and use it in GitHub Desktop.
Result: Calling `Dispose()` on the iterator can dispose the resource inside even the iterator's code seems never return.
void Main()
{
var itor = InfiniteOne();
var iter = itor.GetEnumerator();
iter.MoveNext();
Console.WriteLine("Value is {0}", iter.Current);
iter.Dispose();
}
static IEnumerable<int> InfiniteOne(){
using var _ = new Leakable();
while(true)
yield return 1;
}
sealed class Leakable : IDisposable {
public void Dispose(){
Console.WriteLine("Disposed!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment