Enumerating via interface - performance loss

后端 未结 4 906
情书的邮戳
情书的邮戳 2021-02-13 11:57

I had a little dispute (which was very close to holy war:) ) with my colleage, about the performance of access to list via indeces VS via enumerator. To operat

4条回答
  •  再見小時候
    2021-02-13 12:22

    If you look at the IL for both versions, you will see that the first version uses an iterator of type System.Collections.Generic.List+Enumerator -- a nested struct, which is optimized for iterating over a list.

    The second version uses a generic implementation of System.Collections.Generic.IEnumerator, which is less efficient because it does not "cheat" by keeping a private index to the current item in the list.

提交回复
热议问题