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

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

    It probably depends on the type of collection you are enumerating and the implementation of its indexer. In general though, using foreach is likely to be a better approach.

    Also, it'll work with any IEnumerable - not just things with indexers.

提交回复
热议问题