我有这段代码:
int[] g = new int[3] { 1, 2, 3 };
g.ToList().ForEach(f=>Console.Write(f));
对于数组中的每一项,我想执行一个Action
....
int[]
已经实现Ienumerable
。
我想执行一个没有“ToList()
”的 Action
是否有任何其他解决方案(使用 one 代码行)来做到这一点(没有 toList?即使用其 IEnumerable
特性)
最佳答案
你可以使用 Array.ForEach
相反:
Array.ForEach(g, f => Console.Write(f));
甚至1:
Array.ForEach(g, Console.Write);
不过,我个人可能会使用 foreach
循环来代替 reasons given by Eric Lippert ...
1 如果编译通过。我已经放弃尝试预测方法组转换是否会在泛型类型推断的上下文中起作用。
关于c# - 通过 IEnumerable 特征执行 Action<T>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9925580/