Background: I\'ve got a bunch of strings that I\'m getting from a database, and I want to return them. Traditionally, it would be something like this:
public Li
The only way this would cause problems is if the caller abuses the protocol of IEnumerable
. The correct way to use it is to call Dispose
on it when it is no longer needed.
The implementation generated by yield return
takes the Dispose
call as a signal to execute any open finally
blocks, which in your example will call Dispose
on the objects you've created in the using
statements.
There are a number of language features (in particular foreach
) which make it very easy to use IEnumerable
correctly.