.ForEach() 是 C# 语言的哪一部分?
如果我向某人解释以下 ForEach 功能,那么说 #2 是“LINQ foreach 方法”是否准确,或者它只是一个“List
扩展方法”未与 LINQ 正式关联?
var youngCustomers = from c in customers
where c.Age < 30
select c;
//1. traditional foreach approach
foreach (var c in youngCustomers)
{
Console.WriteLine(c.Display());
}
//2. LINQ foreach approach?
youngCustomers.ToList().ForEach(c => Console.WriteLine(c.Display()));
If I'm explaining the following ForEach feature to someone, is it accurate to say that #2 is the "LINQ foreach approach" or is it simply a "List<T>
extension method" that is not officially associated with LINQ?
var youngCustomers = from c in customers
where c.Age < 30
select c;
//1. traditional foreach approach
foreach (var c in youngCustomers)
{
Console.WriteLine(c.Display());
}
//2. LINQ foreach approach?
youngCustomers.ToList().ForEach(c => Console.WriteLine(c.Display()));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
它是
List
的普通方法,尽管人们经常为其他IEnumerable
提供自己的扩展方法。 LINQ 不提供 ForEach 扩展,因为它的设计目标是由于功能性/使用不可变类型,ForEach 本质上是一种副作用/命令性操作。it is a normal method of
List<T>
though people often provide their own extension methods for otherIEnumerable<T>
. LINQ does not provide a ForEach extension due to its design goals of being functional/ working with immutable types, ForEach is an inherently side effect/imperative operation.它与 LINQ 无关,也不是扩展方法。
ForEach
是一个简单的实例方法List
类。(如果您真的很挑剔,那么
ForEach
根本不是 C# 语言的一部分:它是 .NET 基类库的一部分。)It has nothing to do with LINQ and it's not an extension method.
ForEach
is a plain instance method on theList<T>
class.(And if you wanted to be really nitpicky, then
ForEach
is not part of the C# language at all: it's part of the .NET Base Class Library.)它不是
List
的扩展方法,而是List
的常规方法。所以它与LINQ无关。如果有的话,“正式与 LINQ 关联”和“未正式与 LINQ 关联”之间的区别实际上并不是非常有用。 LINQ 只是一堆链式扩展方法(通常在上的其他扩展方法几乎没有任何区别。最好的区别是它们驻留在 System.Linq 或 System.Something.Linq 命名空间之一。
IEnumerable
上)。它与 IEnumerableIt is not an extension method of
List<T>
, it is a regular method ofList<T>
.So it has nothing to do with LINQ. If it had, the distinction between "officially associated with LINQ" and not officially associated with LINQ is not practically a very useful one. LINQ is simply a bunch of chained extension methods (often on
IEnumerable<T>
). There is rarely any point in distinguishing it from other extension methods onIEnumerable<T>
. The best distinction would be that they reside in one of theSystem.Linq
orSystem.Something.Linq
namespaces.这不是 LINQ。 LINQ 的设计方面之一是标准 LINQ 方法没有副作用。 ForEach 方法会违反这一点。
Eric Lippert 有一篇关于此的博客文章:
链接
It's not LINQ. One of the design aspects of LINQ is that the standard LINQ methods do not have side-effects. The ForEach method would violate this.
Eric Lippert has a blog article all about this:
Link
让我们分解一下:
ToList
是对 .net Framework 3.5 中引入的System.Linq.Enumerable.ToList()
的调用ForEach
是一个调用.net Framework 2.0 中引入的System.Collections.Generic.List.ForEach
c =>
是 c# 3.0 中引入的 lambda 表达式语法。如果您查看
List.ForEach
,您可以看到当时调用它所需的旧委托语法。
Let's break it down:
ToList
is a call toSystem.Linq.Enumerable.ToList()
introduced in .net framework 3.5ForEach
is a call toSystem.Collections.Generic.List<T>.ForEach
introduced in .net framework 2.0c =>
is lambda expression syntax introduced in c# 3.0.If you look at the documentation for
List<T>.ForEach
, you can see the old delegate syntax that was required back then to call it.副作用 foreach 将在 Visual Studio 2010 的 Linq 扩展中提供 ( is )。
Side-effecting foreach will be ( is ) provided in Visual Studio 2010's Linq extensions.