Home > Enterprise >  C# How to get row length from a 2D array with changing row sizes
C# How to get row length from a 2D array with changing row sizes

Time:08-31

I have a 2 dimensional array with rows that change in length. But with .GetLength(1) i only get the length of the first row in the second dimension.

How can i get the size of any specific row in an array?

CodePudding user response:

What you are describing sounds more like a jagged array (array of arrays). That would look like this:

int rowLength = My2dArray[rowIndex].Length;

If you really have a 2D array, then all the rows are required to be the same size, which is set when you first allocate the array. On the other hand, if you do have a jagged array then .GetLength(1) will throw an exception; it does not return the length of the first row as claimed.

Of course, this confusion could be avoided if you showed us how the collection is created and populated.

  • Related