Foreach loop versus ForEach method - Differences?

EDIT: This question may be marked as a duplicate of this question.

Are there any differences (performance or otherwise) between using the foreach loop or the ForEach LINQ method?

For context, this is part of one of my methods:

 foreach (var property in typeof(Person).GetProperties()) { Validate(property.Name); } 

Instead, I can use this code to accomplish the same task:

 typeof(Person) .GetProperties() .ToList() .ForEach(property => Validate(property.Name)); 

When is a loop structure better than a chain of methods?

Here is another example where I used the ForEach method, but could just easily use a foreach loop and a variable:

 // LINQ PrivateData.Database.Users .Cast<User>() .Where(user => user.LoginType == LoginType.WindowsUser) .Select(user => new { Name = user.Name, Login = user.Login }) .ToList() .ForEach(result => WriteObject(result)); // Loop var users = PrivateData.Database.Users .Cast<User>() .Where(user => user.LoginType == LoginType.WindowsUser) .Select(user => new { Name = user.Name, Login = user.Login }); foreach(var user in users) { WriteObject(user); } 
+8
c # foreach linq
source share
3 answers

I would put you off to Eric Lipperts ForEach vs ForEach blog . Like the previous lead developer on the C # compiler team, I think his opinion is in place.

Excerpt: (referring to .ForEach() )

The first reason is that it violates the principles of functional programming on which all other sequence operators are based. Clearly, the only purpose of calling this method is to cause side effects. The purpose of the expression is to calculate the value, and not cause a side effect. The purpose of the application is to cause a side effect. The calling site of this thing would look very similar to an expression (although admittedly, since the method is not valid, an expression can only be used in the context of an expression of an expression of an expression.) It’s not good to do with me the one and only sequence operator that is only useful for it side effects.

+4
source share

The cycle is the best style because it is a tool made just for what you want to do. It blends better with language. For example, you can exit the loop. Tools understand loops; they don't understand ForEach .

The cycle is easier to understand for people. ForEach very unusual.

The loop is also faster because there are fewer indirect calls, fewer delegate distributions, and the optimizer can see more of what you do in one place. It also saves a ToList call. You can save this call by writing your own extension method on IEnumerable<T> .

I think in all cases I think the cycle is better. Maybe there is some kind of angular case where the ForEach method would be a better style or more convenient for some reason.

PLINQ also has ForAll , which is required for performance reasons, because it can be parallelized.

0
source share

In most cases, this is a matter of personal preference. Speaking of performance:

In most cases, LINQ will be a little slower because it introduces overhead. Do not use LINQ if you care about performance. Use LINQ because you need more readable and supported code.

-3
source share

All Articles