difference between [] [] and [] in multidimensional arrays in c #

0

I would like to know if anyone knows what is the difference between these two ways of declaring multidimensional matrices in c# .

string[][] m = new string[2][]; // primera forma

string[,] m2 = new string[2, 2]; // segunda forma

And because when I declare it in the following way:

string[][] m = new string[2][2]; // <- me marca un error

I mark an error by assigning the second length between the second pair of brackets.

  

As a note I want to mention that I always declare them in the second way mentioned in the first block of code.

    
asked by José Gregorio Calderón 10.05.2016 в 09:13
source

1 answer

4

One is a matrix of matrices, and the other is a 2d array.

That is, a double [] [] can be valid like this:

string [] [] matriz = new string [5] [];

matriz [0] = new string [7];
matriz [1] = new string [1];
matriz [2] = new string [666];
matriz [3] = new string [3];
matriz [4] = new string [2];

Because each entry in the array is a reference to another string array. In a staggered way, you can make an assignment to a matrix as you do with the second form.

Another important difference is that the matrix 2d is uniform, and therefore a 1d matrix can not be assigned to a row or column. That is to say, that by obligation you have to give the index of the row and column.

I hope I have resolved your doubt.

    
answered by 10.05.2016 / 09:58
source