/ /インラインのforeach / ifステートメントの何が問題になっていますか? [複製] - c#、配列

私のインラインforeach / ifステートメントで何が問題になっていますか? [複製] - C#、配列

int[][] multi = new int[2][];

multi[0] = new int[2];
multi[1] = new int[2];

multi[0][0] = 11;
multi[0][1] = 2;
multi[0][2] = 4;

multi[1][0] = 4;
multi[1][1] = 5;
multi[1][2] = 6;

Array.ForEach(
multi,
x => multi.Length != x.Length
? throw new Exception("The two dimensional arrays must be symmetrical."));

私は オーバーフロー例外 そして、私がここでやろうとしていることができるかどうかわからない?

回答:

回答№1の場合は3

差し迫った原因 エラーの

multi[0] = new int[2]; // multi[0] has 2 items: with indexes 0 and 1

...

multi[0][2] = 4;       // out of range: multi[0] doesn"t have 3d item (with index 2)

初期化をに変更することができます。

int[][] multi = new int[][] {
new int[] { 11, 2, 4},
new int[] {  4, 5, 6},
};

あなたは一緒に働いています ギザギザの、ない 2D アレイ;それがなぜテストなのか multi することが 平方 配列)

using System.Linq;

...

if (multi.Any(x => x == null || multi.Length != x.Length))
throw new Exception("The two dimensional arrays must be symmetrical.");

リマーク: 捨てないで 一般 Exception、しかし 特定 一つ、言う ArgumentException, ArgumentOutOfRangeException