LINQ vs FOREACH vs FOR Loop Performance

I made a blatantly stupid logic mistake the other day in a business logic layer method that I was writing and got the following exception: “Collection was modified; enumeration operation may not execute.” The problem what I was trying to remove an object from a collection while I was enumerating through it using a FOREACH statement. After I realized what I had done I solved the problem by populating a new collection with a little bit of LINQ.

After fixing my stupid mistake I went down a path of examining the performance differences between using LINQ, FOREACH and a standard FOR loop. I was surprised at the results. I knew that LINQ performance was slower, but I didn't realize exactly how much slower.

This is very import information for anyone who uses LINQ to be aware of:

http://www.schnieds.com/2009/03/linq-vs-foreach-vs-for-loop-performance.html

Aaron
http://www.churchofficeonline.com

No Comments