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

izb picture izb · Nov 5, 2010 · Viewed 80.9k times · Source

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?

Answer

GendoIkari picture GendoIkari · Nov 5, 2010

You want the GetLength() method of your array:

a.GetLength(0);

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