例如
System.Linq的
dogs.ToList().ForEach(dog => Console.WriteLine(dog.Name));
WebGrease.Css.Extensions
dogs.ForEach(dog => Console.WriteLine(dog.Name));
两者似乎做同样的事情,那有什么区别?有任何性能差异吗?另外我认为Linq可能不允许像我上面提到的函数调用,但它确实编译。
答案 0 :(得分:4)
ForEach
是System.List<T>
的成员,不属于Linq。您提供的代码会枚举两次,一次针对ToList
来电,一次针对ForEach
。
WebGrease是IEnumerable<T>
的扩展(请参阅https://searchcode.com/codesearch/view/27951506/)。您为该案例提供的代码将枚举一次。