When you have some enumeration methods in some of your types (i.e. a custom collection), is it better to use LINQ syntax or just old school enumeration (for/foreach)?
Using .NET 3.5 is a given.
I am asking this for maximum performance and readability. The application is also designed to be parallel.
Which one would you favor?
Linq queries can be parallelised easily using PLinq (built into .net 4)
I personally find Linq easier to read, but it depends on you as a developer.
foreach (var item in collection)
{
if (item.Value == SomeOtherValue)
performSomeProcessing();
}
vs
foreach(var item in collection.Where(a => a.Value == SomeOtherValue))
{
performSomeProcessing();
}
I think (although i've not personally benchmarked this) that the compiler optimises out the small function calls, so performance should not be noticably different.
If you absolutely must wring every last drop of performance out, then a for/foreach loop is tough to beat for efficiency. However, if you're going for parallelism and readability, the Enumerable<T> extension methods and LINQ win hands down in my book.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With