3 ms·
I'm not saying to avoid IDisposable (just the opposite in fact). The `foreach` statement and the `using` statement just so happen to desugar to similar code w
by noblethrasher 15y ago
I'm not saying to avoid IDisposable (just the opposite in fact).
The `foreach` statement and the `using` statement just so happen to desugar to similar code when it comes to handling IDisposable. The C# compiler will guarantee that Dispose() is called on the iterator and that code in the `finally` block will execute... even if you exit the loop early due to an `exception` or a break/goto. Just like the `using` statement.
The benefit is that it's even more of a pit-of-success feature than the `using` statement because you can't possibly forget to call Dispose().
It's also only slightly more work for the library author and slightly less work for the client but the client is going to use the code many more times than the author will write it.