Coming from a perl background, I have always defined a 2D array using int[][]
. I know you can use int[,]
instead so what are the differences?

- 15,630
- 17
- 67
- 90
-
1Here are some thoughts in terms of performance: http://stackoverflow.com/questions/168897/whats-better-in-regards-to-performance-type-or-type – bruno conde Jul 21 '09 at 13:39
9 Answers
The difference here is that the first sample, int[][] creates a jagged array, while the second creates a rectangular array (of dimension 2). In a jagged array each "column" can be of a different size. In a true multidimensional array, each "column" (in a dimension) is the same size. For more complete information see the Array section of the C# Programming Guide.

- 1
- 3

- 524,688
- 99
- 697
- 795
Basically int[][] is a "jagged" array, it looks like this:
[] -> [1, 2, 3]
[] -> [1, 2]
[] -> [1, 2, 3, 4]
While int[,] is a multidimentional array which always has the same width and height:
[1, 2, 3, 4
5, 6, 7, 8
9, 1, 2, 3]
Each have their uses depending on what you're trying to accomplish.

- 1
- 1

- 99,986
- 30
- 138
- 174
-
1Jagged--each 'row' can have different 'column' lengths. Multidimensional--each 'row' has the same 'column' length. – Jul 21 '09 at 13:54
int[][] is an array of arrays or "jagged" array: you can use this when you want different sizes in the second dimension. For example, the first sub array can have 5 elements and the second can have 42.
int[,] is a two dimensional array: The second dimension is the same through out the array. With int[7, 42] the second dimension is 42 for all 7 lines.

- 2,790
- 1
- 19
- 35
int[][] is a jagged array, where int[,] is a two dimensional array.
plainly
var a = int[][]
allows you do have an array like this:
a[0] = new int[2];
a[1] = new int[5];
where as with int[,] you must always have the second portion of the array be the same:
var a = int[2,2];
a[0,0]
a[0,1]
a[1,0]
a[1,1]
you can't have a[2,2];

- 113,795
- 27
- 197
- 251
int[][] is called an array of arrays, it can have arbitrary length for each row.
int[,] is called rectangular array, where all the rows have the same length. it Can be simulated by the first one.

- 94,250
- 39
- 176
- 234
The best way to understand the difference is to look at two ways to create an nxn matrix:
const int n = 8, m = 8;
int[,] matrix1 = new int[n,m];
int[][] matrix2 = new int[n][];
for (int i = 0; i < matrix2.Length; i++) { matrix2[i] = new int[m]; }
matrix1[1,1] = matrix2[2][2];
As you can see the second one is a bit more involved because you need the for-loop to fully create it. It is often called a jagged array because the 2nd order arrays do not need to be all of the same length.

- 263,252
- 30
- 330
- 514
One thing to consider about a jagged array is that you are allocating non-contiguous chunks of memory. This is a good thing if you have a large array of large objects. I've seen SomeThing[n,n] create StackOverflow issues, but SomeThing[n][n] be ok.
Also, if an object gets > 85,000 bytes it goes to the LOH (Large Object Heap). .NET Collections and the Large Object Heap (LOH)

- 1
- 1

- 4,699
- 3
- 25
- 34
Here's an excellent article about arrays, covering this topic very well.

- 13,955
- 3
- 43
- 48