Possible to iterate backwards through a foreach?

前端 未结 11 2171
无人及你
无人及你 2020-11-27 03:54

I know I could use a for statement and achieve the same effect, but can I loop backwards through a foreach loop in C#?

11条回答
  •  迷失自我
    2020-11-27 04:36

    When working with a list (direct indexing), you cannot do it as efficiently as using a for loop.

    Edit: Which generally means, when you are able to use a for loop, it's likely the correct method for this task. Plus, for as much as foreach is implemented in-order, the construct itself is built for expressing loops that are independent of element indexes and iteration order, which is particularly important in parallel programming. It is my opinion that iteration relying on order should not use foreach for looping.

提交回复
热议问题