object[,] refers to a rectangular array, which means its grid.
Then you have object[][] , which is an array with notches, an array of arrays.
The main difference is that object[,] will always have fixed sizes, and with an uneven array ( object[][] ), all arrays can have different sizes.
This is an example that clearly shows the difference in usage (both do the same):
// Create and fill the rectangluar array int[,] rectangularArray = new int[10, 20]; for (int i = 0; i < 200; i++) rectangularArray[i / 20, i % 20] = i; // Next line is an error: // int[][] jaggedArray = new int[10][20]; int[][] jaggedArray = new int[10][]; // Initialize it // Fill the jagged array for (int i = 0; i < 200; i++) { if (i % 20 == 0) jaggedArray[i / 20] = new int[20]; // This size doesn't have to be fixed jaggedArray[i / 20][i % 20] = i; } // Print all items in the rectangular array foreach (int i in rectangularArray) Console.WriteLine(i); // Print all items in the jagged array // foreach (int i in jaggedArray) <-- Error foreach (int[] innerArray in jaggedArray) foreach (int i in innerArray) Console.WriteLine(i);
EDIT:
Warning, this code above is not real production code, it is simply the best way to do this as an example.
Intensive use of divisions through / and % makes it much slower. You can better use a nested loop loop.
Aidiakapi
source share