7

I seem to be getting an odd value.

How do I get the number of rows in my array:

double[,] lookup = { {1,2,3}, {4,5,6} };

The output should be 2.

Blankman
  • 259,732
  • 324
  • 769
  • 1,199
  • +1 I'd forgotten or did not know that c# supported multidimensional arrays. Thanks for the reminder. – Brian Mar 24 '09 at 15:36
  • I knew of them but I still hadn't any case where I could use them. Maybe in more "mathematical" situations (matrices, vectors, etc.). – mmmmmmmm Mar 24 '09 at 18:13

3 Answers3

18

lookup has two dimensions, this is how you can read them

double[,] lookup = { {1,2,3}, {4,5,6} };

int rows = lookup.GetLength(0); // 2
int cols = lookup.GetLength(1); // 3    
int cells = lookup.Length;      // 6 = 2*3

The concept of rows and cols is just tradition, you might just as well call the first dimension the columns.

Also see this question

Community
  • 1
  • 1
H H
  • 263,252
  • 30
  • 330
  • 514
3

You want the rank property on the array...

double[,] lookup = { { 1, 2, 3 }, { 4, 5, 6 } };
Console.WriteLine(lookup.Rank);

This will provide you with the number of dimensions.

Edit:
This will only provide you with the number of dimensions for the array as opposed to the number of primary elements or "rows" see @Henk Holterman's answer for a working solution.

Quintin Robinson
  • 81,193
  • 14
  • 123
  • 132
1

I think what you're looking for is:

lookup.GetLength(0);
BFree
  • 102,548
  • 21
  • 159
  • 201