c# 中的这个 foreach 是怎么回事?
(编辑:代码稍微整洁。)
像这样使用 foreach 效果很好。
var a = new List<Vector2>();
a.ForEach(delegate(Vector2 b) {
b.Normalize(); });
然而,以下内容会导致“方法‘ForEach’没有重载需要 1 个参数”。
byte[,,] a = new byte[2, 10, 10];
a.ForEach(delegate(byte b) {
b = 1; });
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您已对数组使用了
List.ForEach()
方法的语法,但Array.ForEach()
语法是:一个重要的一点是数组应该是一个-维以便在
Array.ForEach()
中使用它。考虑到这一点,我建议使用简单的for
循环You've used syntax of
List.ForEach()
method for the array, butArray.ForEach()
syntax is:One important point that array should be one-dimensional in order to use it in
Array.ForEach()
. Considering this I would suggest using simplefor
loop我不知道采用多维数组的 ForEach 方法。
如果你想要一个,我想你必须自己创建它。
操作方法如下:
示例程序,使用新的 ForEach 方法:
另外,
Array
版本中的ForEach
方法不是实例方法,而是 statis 方法。你这样称呼它:I don't know of a ForEach method that takes multi-dimensional arrays.
If you want one, i think you will have to create it yourself.
Here is how to do it:
Sample program, using the new ForEach method:
Also, the
ForEach
method in theArray
version, is not an instance method, it is statis method. You call it like this:原始数组的实例方法比通用集合少得多,因为它们不是模板化的。这些方法(例如
ForEach()
或Sort()
)通常作为静态方法实现,这些方法本身就是模板化的。在这种情况下,Array.Foreach(a, action) 将为数组完成任务。
当然,经典的 foreach(var b in a) 对于 List 和 Array 都适用,因为它只需要一个枚举器。
但是:
(b=1)
将不起作用。因为您收到的是一个值,而不是一个引用。raw arrays have much less instance methods than generic collections because they are not templated. These methods, such as
ForEach()
orSort()
are usually implemented as static methods which are themselves templated.In this case,
Array.Foreach(a, action)
will do the trick for the array.Of course, the classical
foreach(var b in a)
would work for both List and Array since it only requires an enumerator.However:
(b=1)
won't work. Because you receive a value, not a reference.List
有第一个实例方法。数组则不然。List
has the first instance method. Arrays do not.我建议您只使用普通的
foreach
循环来转换数据。您使用的方法仅存在于List
实现中,而不存在于数组中。使用 foreach 方法实际上不会给你带来任何好处,除非由于某种原因你想在方法链中进行数据突变。在这种情况下,您也可以为
IEnumerable
编写自己的扩展方法。不过,我建议不要这样做。有一个单独的 foreach 循环可以让读者清楚地知道数据正在发生变化。它还消除了为循环的每次迭代调用委托的开销。无论集合类型如何,只要它是 IEnumerable ,它都可以工作(不完全正确,您可以编写自己的枚举器和枚举,但这是一个不同的问题)。
如果您只想进行数据转换(即投影等),那么请使用 LINQ。
另请记住,对于数组,您将获得字节的副本而不是引用。您将仅修改该字节而不是原始字节。这是一个输出示例:
产生输出:
正如您所看到的,lambda 中的数字 += 1 没有任何效果。事实上,如果您在正常的
foreach
循环中尝试这样做,您会收到编译器错误。I would recommend you just use a normal
foreach
loop to transform the data. You're using a method that exists only on theList<T>
implementation, but not on arrays.Using a method for
foreach
really gains you nothing, unless for some reason you were wanting to do data mutation in a method chain. In that case, you may as well write your own extension method forIEnumerable<T>
. I would recommend against that, though.Having a separate
foreach
loop makes it clear to the reader that data mutation is occurring. It also removes the overhead of calling a delegate for each iteration of the loop. It will also work regardless of the collection type as long as it is anIEnumerable
(not entirely true, you can write your own enumerators and enumerables, but that's a different question).If you're looking to just do data transformations (i.e. projections and the like) then use LINQ.
Also keep in mind that with the array, you're getting a copy of the
byte
not a reference. You'll be modifying just thatbyte
not the original. Here's an example with the output:Which yields the output:
As you can see, the number += 1 in the lambda had no effect. In fact, if you tried this in a normal
foreach
loop, you would get a compiler error.您正在使用两个不同的
ForEach
。Array.ForEach 在
byte[,,] 示例(尽管您使用不正确)和 List<...>
示例中的 rel="nofollow">List.ForEach。You're using two different
ForEach
'es.Array.ForEach in the
byte[,,]
example (though you're using it incorrectly) and List.ForEach in theList<...>
example.