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

前端 未结 30 1399
抹茶落季
抹茶落季 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:29

    It seems a bit strange to totally forbid the use of something like a for loop.

    There's an interesting article here that covers a lot of the performance differences between the two loops.

    I would say personally I find foreach a bit more readable over for loops but you should use the best for the job at hand and not have to write extra long code to include a foreach loop if a for loop is more appropriate.

提交回复
热议问题