# Length of a Multi-Dimensional Array

• ### Question

• What's the length of this array?

int[,] n6 = { {1, 2}, {3, 4}, {5, 6} };

Tuesday, September 18, 2012 6:24 PM

• Why don't you ask C# itself? The following code prints 3 different answers, pick whichever you consider to be length in this case:

```            int[,] n6 = { { 1, 2 }, { 3, 4 }, { 5, 6 } };

Console.WriteLine(n6.Length); // this says 6
Console.WriteLine(n6.GetLength(0)); // this says 3
Console.WriteLine(n6.GetLength(1)); // and this says 2
```

• Proposed as answer by Tuesday, September 18, 2012 6:30 PM
• Marked as answer by Tuesday, September 18, 2012 6:43 PM
Tuesday, September 18, 2012 6:29 PM

### All replies

• Why don't you ask C# itself? The following code prints 3 different answers, pick whichever you consider to be length in this case:

```            int[,] n6 = { { 1, 2 }, { 3, 4 }, { 5, 6 } };

Console.WriteLine(n6.Length); // this says 6
Console.WriteLine(n6.GetLength(0)); // this says 3
Console.WriteLine(n6.GetLength(1)); // and this says 2
```

• Proposed as answer by Tuesday, September 18, 2012 6:30 PM
• Marked as answer by Tuesday, September 18, 2012 6:43 PM
Tuesday, September 18, 2012 6:29 PM
• In addition to Mike's answer - Array.Rank (http://msdn.microsoft.com/en-us/library/system.array.rank.aspx) provides the number of dimensions, in case that's not known.

Reed Copsey, Jr. - http://reedcopsey.com