In short, you don't need to worry about it as long as the caller of the method handles the IEnumerator
objects properly.
IEnumerator
implementes IDisposable
, 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 the using
call, and that is where the IDisposable
resource is disposed.
So as long as the IEnumerator
objects created from this IEnumerable
are either iterated fully (in which case the final MoveNext
call will reach the end of the using
block and dispose of the resource) or disposed the IDisposable
client will be disposed.
Note that if you're concerned the user of your code may not treat the IEnumerator
objects 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.