Is there a proper way to yield through a disposable resource? The returned objects are IDisposable, but the element it is iterating through is.
Here is an example:
public static IEnumerable<T> Fetch(IEnumerable<Guid> ids)
{
using (var client = new CouchbaseClient())
{
yield return ids.Select(s => s.ToString());
}
}
Right now, calling this would not dispose the using resource obtained. I know I can just to a ToList and return it all at once, but is there a way to handle this “properly”, or do I have to keep a tab on the IDisposable resource and dispose of it manually when I’m done?
In short, you don’t need to worry about it as long as the caller of the method handles the
IEnumeratorobjects properly.IEnumeratorimplementesIDisposable, and the logic used in creating the iterator blocks is actually smart enough to execute all un-executed finally blocks when it is disposed. A finally block is created as a result of theusingcall, and that is where theIDisposableresource is disposed.So as long as the
IEnumeratorobjects created from thisIEnumerableare either iterated fully (in which case the finalMoveNextcall will reach the end of theusingblock and dispose of the resource) or disposed theIDisposableclient will be disposed.Note that if you’re concerned the user of your code may not treat the
IEnumeratorobjects properly then you’re best bet is to not use an iterator block with lazy evaluation. If you want to ensure that even if the caller doesn’t “play nice” then eagerly evaluate the method (i.e. take the code you have, dump the results into a list, and then return that list). If the consequences of not disposing the resource are primarily, or entirely performance related (not releasing some memory for a while longer, keeping open a connection, etc.) then it may not be a concern, but if holding onto the lock forever is a major problem (i.e. a locked resource that could result in deadlocks if not released) then the advantage of lazy evaluation may not be worth it.