Exactly and that’s the purpose of the first implementation. If there’s nothing to dispose, there should not be a try/finally block. You can see in SharpLab that no try/finally block is added.

When implementing IEnumerable<T>, it forces the enumerator to implement IDisposable, because IEnumerator<T> derives from it. You can play with the code in SharpLab and you’ll find that there’s no way to get rid of it. In ‘JIT Asm’ mode, you can see that Dispose() is being called (no JIT optimisation occurred). In ‘IL’ mode, you can see that the call is ‘constrained’ to the enumerator type, making it a direct call even after casted to IDisposable.

Principal Engineer @ Farfetch - Innovation Lab https://about.me/antao.almada

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store