foreach vs LINQ .ForEach() [英] foreach vs LINQ .ForEach()
本文介绍了foreach vs LINQ .ForEach()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
.ForEach
需要一个 List
对象,而 foreach 语句只需要 IEnumerable
?
我更喜欢使用 foreach 当有多行和单行语句LINQ - 只是为了可读性。
但什么是首选,为什么?为什么区别?
解决方案没有Linq ForEach()
方法。您正在查看的方法是成员列表< T>
。
。
Why does LINQ .ForEach
require a List
object, while the foreach
statement only requires IEnumerable
?
I prefer using foreach
when having multiple lines and LINQ for single line statements - just for readability.
But what is preferred, and why? And why the difference?
解决方案 There is no Linq ForEach()
method. The method you are looking at is a member of List<T>
.
See here for a discussion on why Linq/IEnumerable doesn't have ForEach()
.
这篇关于foreach vs LINQ .ForEach()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文