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

C#.NetArraysMultidimensional Array

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

You want the GetLength() method of your array:

a.GetLength(0);

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

Solution 2 - C#

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

Solution 3 - C#

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionizbView Question on Stackoverflow
Solution 1 - C#GendoIkariView Answer on Stackoverflow
Solution 2 - C#Brian RasmussenView Answer on Stackoverflow
Solution 3 - C#Paul WilliamsView Answer on Stackoverflow