Just wondering why Enumerable.Range implements IDisposable.
I understand why IEnumerator<T> does, but IEnumerable<T> doesn’t require it.
(I discovered this while playing with my .Memoise() implementation, which has statement like
if (enumerable is IDisposable)
((IDisposable)enumerable).Dispose();
in its “source finished” method that I had placed a breakpoint on out of curiousity, and was triggered by a test.)
Enumerable.Rangeusesyield returnin its method body. Theyield returnstatement produces an anonymous type that implementsIDisposable, under the magic of the compiler, like this:After being compiled, there is an anonymous nested class like this:
so the result
isaIDisposable. In this example, theDisposemethod leaves empty. I think the reason is that there is nothing need to be disposed. If youyield returna type that contains unmanaged resources, you may get a different compiling result. (NOT SURE about it)