【发布时间】:2013-09-09 22:04:06
【问题描述】:
using System;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int[,] x = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }, { 10, 11, 12 } };
foreach (var i in x)
{
foreach (var j in i)
{
Console.WriteLine(j);
}
}
}
}
}
我注意到var i in x 将数组展平,因此它会为第二个foreach 生成以下错误。
错误 1 foreach 语句不能对“int”类型的变量进行操作 因为“int”不包含“GetEnumerator”的公共定义
是否可以防止foreach 扁平化矩形阵列?
【问题讨论】:
-
看起来你正在寻找一个锯齿状数组,而不是一个多维数组:
int[][] x = new int[][] { new[] { 1, 2, 3 }, new[]{ 4, 5, 6 }, new[]{ 7, 8, 9 }, new[]{ 10, 11, 12 } };
标签: c#