0

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?

carlos.gmz
  • 100
  • 8
  • 3
    _"with changing row sizes"_ - are you sure you don't have a jagged array? A 2d array doesn't have changing row sizes. You can see the difference here: [What are the differences between a multidimensional array and an array of arrays in C#?](https://stackoverflow.com/questions/597720/what-are-the-differences-between-a-multidimensional-array-and-an-array-of-arrays) – ProgrammingLlama Aug 30 '22 at 13:20
  • Can you [edit] your post with some example code (a [mre])? – gunr2171 Aug 30 '22 at 13:22
  • In dont mean changing row sizes like changing in real time, but the first row can have a size of 10 and the second row a size of maby 15 and so on. Is that more understandable? – carlos.gmz Aug 30 '22 at 13:22
  • 3
    @carlos _"Is that more understandable?"_ - no, because I understood the same information this time as last time, so it's equally understandable. **But** that sounds like a jagged array, not a 2D array. Please edit your post with some code. – ProgrammingLlama Aug 30 '22 at 13:26

1 Answers1

2

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.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794