开发者

How do I find the size of a 2D array? [duplicate]

This question already has answers here: How do you get the width and height of a multi-dimensional array? (6 answers) Closed 4 years ago.

If I declare this array...

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

Then I can measure the length with

a.Length

which is 12. How do I measure the dimension of the arrays within? If I try...

a[0].Length

I get Wrong number of indices inside []; expected 2.开发者_如何学运维 What gives?


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.


Use System.Array.GetLength(int dimension).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜