Do LINQ IEnumerable extensions call Dispose on their IEnumerable?

后端 未结 1 660
长情又很酷
长情又很酷 2020-12-15 20:57

Given I\'ve written a class that implements IEnumerable, and returns an IEnumerator that\'s not just IDisposable by nature of being an

1条回答
  •  失恋的感觉
    2020-12-15 21:29

    Yes, you can rely on the call of Dispose() on your iterators from inside the methods of LINQ.

    In the reference code published by Microsoft, the use of iterators falls in three categories:

    • Iterators are used from within a foreach loop, which ensures a call of Dispose(), or
    • Iterators are used in conjunction with a while loop; these iterators are disposed explicitly
    • Iterators are obtained inside a using block, which ensures automatic disposal.

    In all these cases the library ensures that iterator's Dispose() method is called upon completion.

    0 讨论(0)
提交回复
热议问题