问题描述
大家好,
在c#中迭代多维数组的最佳方法是什么。
我已将我的数组定义为
Hi All,
What would be the best way to iterate through a multidimensional array in c#.
I have defined my array as
new string[,,] { { { "A", "B", "C" }, { "D", "E", "F" } } }
但是我无法绕过foreach循环过程。
我希望能够将此数组作为变量传递,然后在迭代数组时分配每个值。
提前感谢。
But I am having trouble getting my head around the process of a foreach loop.
I want to be able to pass this array as a variable then assign each value as it iterates through the array.
Thanks in advance.
推荐答案
string[,,] arr = new string[,,] { { { "A", "B", "C" }, { "D", "E", "F" } } };
foreach(string k in arr)
{
System.Console.Write("{0} ", k);
}
但是MSDN建议使用带有多维数组的嵌套for循环
使用多维数组,使用嵌套的for循环可以更好地控制数组元素。
阅读这些帖子,
[]
[]
[]
But MSDN recommends to use nested for loops with multidimensional arrays
"with multidimensional arrays, using a nested for loop gives you more control over the array elements."
Read these posts,
http://msdn.microsoft.com/en-us/library/2h3zzhdw.aspx[^]
http://stackoverflow.com/questions/2834233/how-to-foreach-through-a-2-dimensional-array[^]
http://stackoverflow.com/questions/2834233/how-to-foreach-through-a-2-dimensional-array[^]
这篇关于迭代c#中的多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!