这应该工作:
foreach (string s in cross.SelectMany(x => x.Cast<string>()))
{
// Code goes here.
}
UPDATE:基于您的评论,它看起来像你想为你的枚举在某些时候对付string[]
,看起来像这样:
{"1", "b", "b", "b"}
的问题是:您声明的数组中不存在这样的数组。这可能会令人困惑,因为用于声明T[]
数组的语法与用于声明T[,]
数组的语法之间存在重叠。
让我们写出你的初始化,使其更清晰:
string[][,] cross = {
new string[,] {
{"1", "b", "b", "b"},
{"b", "c", "c", "c"}
},
new string[,] {
{"2", "b", "b", "e"},
{"b", "c", "c", "d"}
}
};
我们这里是什么尺寸的4×2两种string[,]
阵列。表达式{"1", "b", "b", "b"}
高于并不代表单个阵列,而是多维数组的一维中的值。
为了达到你想要的行为,Mark Cidade's answer是对的钱:你不能这样做与string[][,]
,但你可以做到这一点与string[][][]
。
string[][][] cross = new[] {
new[] {
new[] {"1", "b", "b", "b"},
new[] {"b", "c", "c", "c"}
},
new[] {
new[] {"2", "b", "b", "e"},
new[] {"b", "c", "c", "d"}
}
};
以上述方式声明cross
可以让你做到以下几点:
foreach (string[][] abc in cross)
{
foreach (string[] arr in abc)
{
Console.WriteLine(string.Join(", ", arr));
}
}
或者,从我原来的建议借:
foreach (string[] arr in cross.SelectMany(x => x))
{
Console.WriteLine(string.Join(", ", arr));
}
输出:
1, b, b, b
b, c, c, c
2, b, b, e
b, c, c, d
您目前具有2D阵列的锯齿形阵列。** – 2010-09-03 12:37:13
我建议使用对象或字符串来代替3D阵列的列表,将通过 – Gage 2010-09-03 13:22:44