In .NET, which loop runs faster, 'for' or 'foreach'?

前端 未结 30 1884
抹茶落季
抹茶落季 2020-11-22 04:25

In C#/VB.NET/.NET, which loop runs faster, for or foreach?

Ever since I read that a for loop works faster than a foreach

30条回答
  •  梦如初夏
    2020-11-22 04:53

    you can read about it in Deep .NET - part 1 Iteration

    it's cover the results (without the first initialization) from .NET source code all the way to the disassembly.

    for example - Array Iteration with a foreach loop:

    and - list iteration with foreach loop:

    and the end results:

提交回复
热议问题