foreach循环与ForEach方法 - 差异? [英] foreach loop vs. ForEach method - Differences?

查看:184
本文介绍了foreach循环与ForEach方法 - 差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:这个问题可以标记为这个问题。



使用foreach循环或 ForEach LINQ 方法?

对于上下文,这是我的一个方法的一部分:

 foreach(typeof(Person).GetProperties()中的var属性)
{
Validate(property.Name);
}

我也可以使用这段代码执行相同的任务:



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

何时使用循环结构比使用方法链更好? b

下面是使用 ForEach 方法的另一个示例,但可以轻松使用foreach循环和变量:


$ b $ pre code $ LINQ
PrivateData.Database.Users
Cast< User>()
.Where user => user.LoginType == LoginType.WindowsUser)
.Select()$ b $ .Select(user => new {Name = user.Name,Login = user.Login}) b .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用户)
{
WriteObject(user);


解决方案

Eric Lipperts的博客foreach与ForEach 。作为C#编译器团队的前一个主要开发人员,我认为他的观点是现实的。



摘录:(引用<$ c $第一个原因是这样做违反了所有的函数式编程原则,即所有的其他序列操作符是基于的。很明显,调用这个方法的唯一目的就是引起副作用。表达式的目的是计算一个值,而不是引起副作用。声明的目的是引起副作用。这个东西的调用网站看起来像一个表达式很糟糕(虽然,诚然,由于该方法是无效的返回,该表达式只能用于语句表达上下文)。这不适合我使一个唯一的序列操作符只对其副作用有用。


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

Is there any differences (performance or otherwise) between using a 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);
}

I can alternatively use this code to perform the same task:

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

When would be using the loop structure be better than using method chaining?

Here's another example where I've used the ForEach method, but could just have easily used 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);
}

解决方案

I would defer you to Eric Lipperts blog "foreach" vs "ForEach". As the previous principal developer on the C# compiler team, I think his opinion is spot on.

Excerpt: (referring to .ForEach())

The first reason is that doing so violates the functional programming principles that all the other sequence operators are based upon. Clearly the sole purpose of a call to this method is to cause side effects. The purpose of an expression is to compute a value, not to cause a side effect. The purpose of a statement is to cause a side effect. The call site of this thing would look an awful lot like an expression (though, admittedly, since the method is void-returning, the expression could only be used in a "statement expression" context.) It does not sit well with me to make the one and only sequence operator that is only useful for its side effects.

这篇关于foreach循环与ForEach方法 - 差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆