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

做~自己de王妃 提交于 2019-12-28 11:43:45

问题


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?


回答1:


You want the GetLength() method of your array:

a.GetLength(0);

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




回答2:


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




回答3:


Use System.Array.GetLength(int dimension).



来源:https://stackoverflow.com/questions/4106369/how-do-i-find-the-size-of-a-2d-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!