C# 如何从 foreach 循环内修改 foreach 迭代变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11810794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 19:21:12  来源:igfitidea点击:

How to modify a foreach iteration variable from within foreach loop?

c#foreachiterationvariable-assignment

提问by user1306322

When I try to do this...

当我尝试这样做时...

Item[,] array = new Item[w, h];  // Two dimensional array of class Item, 
                                 //   w, h are unknown at compile time.
foreach(var item in array)
{
    item = new Item();
}

...I get Cannot assign to 'item' because it is a 'foreach iteration variable'.

...我明白了Cannot assign to 'item' because it is a 'foreach iteration variable'

Still, I'd like to do that.

尽管如此,我还是想这样做。

The idea is to assign default Itemclass values to existing item.

这个想法是Item为现有项目分配默认类值。

采纳答案by Jon Skeet

Okay, now that we know your aiminstead of how you were trying to achieve it, it's much easier to answer your question: you shouldn't be using a foreachloop. foreachis about readingitems from a collection - not changing the contents of a collection. It's a good job that the C# compiler makes the iteration variable read-only, otherwise it would have let you change the value of the variablewithout that actually changing the collection. (There'd have to be more significant changes to allow changes to be reflected...)

好的,既然我们知道您的目标而不是您尝试实现它的方式,那么回答您的问题就容易多了:您不应该使用foreach循环。foreach是关于从集合中读取项目 - 不更改集合的内容。C# 编译器将迭代变量设置为只读是一项很好的工作,否则它会让您更改变量的值而不实际更改集合。(必须有更重大的变化才能反映变化......)

I suspect you just want:

我怀疑你只是想要:

for (int i = 0; i < array.GetLength(0); i++)
{
    for (int j = 0; j < array.GetLength(1); j++)
    {
        array[i, j] = new Item();
    }
}

That's assuming it's a rectangular array (an Item[,]). If it's an Item[][]then it's an array of arrays, and you'd handle that slightly differently - quite possibly with a foreachfor the outer iteration:

假设它是一个矩形数组(an Item[,])。如果它是一个,Item[][]那么它是一个数组数组,你会稍微不同地处理它 - 很可能用 aforeach表示外部迭代:

foreach (var subarray in array)
{
    for (int i = 0; i < subarray.Length; i++)
    {
        subarray[i] = new Item();
    }
}

回答by Pompair

You can use normal for loop for that.

您可以为此使用普通的 for 循环。

回答by Joe Shanahan

Not knowing the size isn't a problem:

不知道大小不是问题:

for (int i = 0; i < twoDimArray.GetLength(0); i++)
{
    for (int j = 0; j < twoDimArray.GetLength(1); j++)
    {
        twoDimArray[i, j] = ...
    }
}

回答by Adam Robinson

It looks like you're trying to initialize the array. You can't do that this way. Instead, you need to loop through the array by index.

看起来您正在尝试初始化数组。你不能这样做。相反,您需要按索引遍历数组。

To initialize the elements of a given two-dimensional array, try this:

要初始化给定二维数组的元素,请尝试以下操作:

for (int d = 0; d < array.GetLength(0); d++)
{
    for (int i = 0; i < array.GetLength(1); i++)
    {
        array[d, i] = new Item();
    }
}