如何找到2D 数组的大小?

如果我声明这个数组。

string[,] a = {
{"0", "1", "2"},
{"0", "1", "2"},
{"0", "1", "2"},
{"0", "1", "2"},
};

然后我可以用

a.Length

也就是12。我如何测量内部数组的维数? 如果我尝试..。

a[0].Length

我得了 Wrong number of indices inside []; expected 2怎么了?

89005 次浏览

You want the GetLength() method of your array:

a.GetLength(0);

http://msdn.microsoft.com/en-us/library/system.array.getlength.aspx

Use Array.Rank to get number of dimensions and then use Array.GetLength(int dimension) to get the length of a specific dimension.