CodingLambdas CodingLambdas - 1 month ago 10
C# Question

the difference between [,] and [][] in C#

I am currently trying to learn C#, and I ran across following question:

What is the difference between

[,]
and
[][]
in a type in C#?

Answer

[,] form creates rectangular two dimensional array. When you specify the dimensions you will get a two dimensional matrix of those sizes. All its elements are initialized to the default value of the element type.

int[,] array = new int[4, 2];

[][] is called jagged array and it is basically an array of arrays. When you create it, you first only specify the size of the first dimension:

int[][] jaggedArray = new int[3][];

Now you have to create a new array for each of the rows manually (they are initially null)

jaggedArray[0] = new int[10];
jaggedArray[1] = new int[6];

The advantage of this is that each row can have a different length.