89

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?

Hamid Pourjam
  • 20,441
  • 9
  • 58
  • 74
izb
  • 50,101
  • 39
  • 117
  • 168

3 Answers3

114

You want the GetLength() method of your array:

a.GetLength(0);

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

GendoIkari
  • 11,734
  • 6
  • 62
  • 104
64

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

Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317
17

Use System.Array.GetLength(int dimension).

Paul Williams
  • 16,585
  • 5
  • 47
  • 82