Difference between foreach and for loops over an IEnumerable class in C#

后端 未结 7 939
有刺的猬
有刺的猬 2020-12-17 17:00

I have been told that there is a performance difference between the following code blocks.

foreach (Entity e in entityList)
{
 ....
}

and <

7条回答
  •  攒了一身酷
    2020-12-17 17:09

    Here is a good article that shows the IL differences between the two loops.

    Foreach is technically slower however much easier to use and easier to read. Unless performance is critical I prefer the foreach loop over the for loop.

提交回复
热议问题