执行动作< T>通过IEnumerable的特点? [英] Execute Action<T> by IEnumerable Characteristics?

查看:128
本文介绍了执行动作< T>通过IEnumerable的特点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个code:

 int[] g = new int[3] { 1, 2, 3 };
 g.ToList().ForEach(f=>Console.Write(f));

对于数组中的每一个项目,我想执行动作 ...

INT [] 实施的IEnumerable

我想执行无动作了ToList()

还有没有其他的解决方案(是一个线code),以利用其的IEnumerable 特性做(没有了ToList?即)

is there any other solution ( with one line of code) to do it ( without toList? i.e using its IEnumerable characteristics )

推荐答案

您可以使用 Array.ForEach 来代替:

You could use Array.ForEach instead:

Array.ForEach(g, f => Console.Write(f));

甚至 1

Array.ForEach(g, Console.Write);

我个人很可能使用的foreach 循环,而不是不过,为的 ...

Personally I'd probably use a foreach loop instead though, for the reasons given by Eric Lippert...

1 如果编译。我已经放弃了试图predict方法组转换是否将工作在通用的类型推断的背景下。

1 If it compiles. I've given up trying to predict whether method group conversion will work in the context of generic type inference.

这篇关于执行动作< T>通过IEnumerable的特点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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