I'd like to know what the suggested pattern for cleanup is when working with IAsyncEnumerable.
I know that Task implements IDisposable but it's not necessary to use Dispose in that case based on @stephentoub 's excellent article - https://devblogs.microsoft.com/pfxteam/do-i-need-to-dispose-of-tasks/. Is it the same case for IAsyncEnumerable (i.e. don't worry about disposing) ?
Any suggestions are appreciated! TIA
⚠Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
IAsyncEnumerator is an IAsyncDisposable, just as IEnumerator is an IDisposable. And you want to dispose it at similar times. await foreach will dispose the async enumerator it gets just at foreach will dispose the enumerator it gets. Etc. Just as IEnumerable isn't IDisposable, IAsyncEnumerable isn't either.
Thanks for adding this issue @udlose
@stephentoub's comment is the right fix.
I've added this to our backlog, and I've added the up-for-grabs label for any community members that wish to make a contribution to address this.
IAsyncEnumerator is an IAsyncDisposable, just as IEnumerator is an IDisposable. And you want to dispose it at similar times. await foreach will dispose the async enumerator it gets just at foreach will dispose the enumerator it gets. Etc. Just as IEnumerable isn't IDisposable, IAsyncEnumerable isn't either.
Thanks for pointing out the error in my question as well. I was incorrectly asking about IAsyncEnumerable being disposable - not IAsyncEnumerator. I completely forgot that IEnumerator is an IDisposable - doh! I probably didn't remember since I don't remember having to create a custom IEnumerator. The foreach handling the call to Dispose or DisposeAsync totally makes it a "black-box" for the user (which makes it so much easier to use).
Now the parallels between the async and non-async versions make sense. Thanks for clarifying.