Why does IEnumerator inherit from IDisposable while the non-generic IEnumerator does not?

前端 未结 6 1970
旧时难觅i
旧时难觅i 2020-12-01 01:25

I noticed that the generic IEnumerator inherits from IDisposable, but the non-generic interface IEnumerator does not. Why is it designed in this way?

6条回答
  •  天涯浪人
    2020-12-01 01:36

    I know this is an old discussion but I reasontly wrote a library where I used IEnumerable of T/IEnumerator of T where users of the library could implement custom iterators they should just implement IEnumerator of T.

    I found it very strange that IEnumerator of T would inherit from IDisposable. We implement IDisposable if we want to free unmanged resources right? So it would only be relevant for enumerators that actually hold unmanaged resources - like an IO stream etc. Why not just let users implement both IEnumerator of T and IDisposable on their enumerator if it makes sense? In my book this violates the single responsibility principle - Why mix enumerator logic and disposing objects.

提交回复
热议问题