foreach vs LINQ .ForEach() [英] foreach vs LINQ .ForEach()

查看:140
本文介绍了foreach vs LINQ .ForEach()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么LINQ .ForEach 需要一个 List 对象,而 foreach 语句只需要 IEnumerable



我更喜欢使用 foreach 当有多行和单行语句LINQ - 只是为了可读性。

但什么是首选,为什么?为什么区别?

解决方案

没有Linq ForEach()方法。您正在查看的方法是成员列表< T>

ForEach ()


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屋!

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