Last active
December 29, 2022 12:12
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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