Skip to content
Theory · Arrays · lesson 9 of 9

Multidimensional and jagged arrays

So far the lockers stood in one row. Left-luggage lockers at a train station, though, often form a wall: several rows one above another. To find a locker you need two numbers, the row and the place in the row. A table, a game board and the seats in a theater work the same way.

C# keeps such data in two ways:

  • in a rectangular array, where all rows have the same length;
  • in an array of arrays, where each row has a length of its own.

A rectangular array

A rectangular array is declared with a comma inside the square brackets: int[,]. Such an array is called multidimensional: it has two dimensions, rows and columns.

C#
int[,] grid = new int[2, 3];
grid[0, 1] = 5;
Console.WriteLine(grid[0, 1]);        // 5
Console.WriteLine(grid[1, 0]);        // 0
Console.WriteLine(grid.Length);       // 6
Console.WriteLine(grid.GetLength(0)); // 2
Console.WriteLine(grid.GetLength(1)); // 3

Let's go through it line by line:

  1. new int[2, 3] creates two rows of three elements each, and every element holds zero.
  2. grid[0, 1] = 5 puts 5 into row 0, column 1: the row number comes first, then the column number.
  3. Nobody changed grid[1, 0], so it holds 0.
  4. grid.Length is the number of all elements: 2 * 3, that is, 6.
  5. grid.GetLength(0) is the number of rows, 2, and grid.GetLength(1) is the number of columns, 3.

You can give the values right away, row by row in curly braces. Values listed in square brackets, as for an ordinary array, do not work here: the compiler reports error CS9174.

You walk through such an array with two nested loops:

C#
int[,] seats = { { 1, 2, 3 }, { 4, 5, 6 } };
for (int r = 0; r < seats.GetLength(0); r++)
{
    int sum = 0;
    for (int c = 0; c < seats.GetLength(1); c++)
    {
        sum += seats[r, c];
    }
    Console.WriteLine(sum);
}
// 6
// 15

The outer loop goes over the rows, and the inner one over the columns of one row. For each row the program prints its sum: 1 + 2 + 3 and 4 + 5 + 6.

A jagged array

In an array of arrays, each row is a separate ordinary array, and the rows can have different lengths. Such an array is called jagged: its right edge is uneven, like the teeth of a saw. The type int[][] reads "an array whose every element is an int[] array".

C#
int[][] rows = [[1, 2], [3], [4, 5, 6]];
Console.WriteLine(rows.Length);    // 3
Console.WriteLine(rows[2].Length); // 3
Console.WriteLine(rows[2][1]);     // 5
foreach (int[] row in rows)
{
    Console.WriteLine(string.Join(" ", row));
}
// 1 2
// 3
// 4 5 6

Let's see what the program printed:

  1. rows.Length is the number of rows, 3.
  2. rows[2] is the third row, an array of its own, [4, 5, 6], and its length is 3 too.
  3. rows[2][1] takes row 2 first, and element 1 in it: that is 5.
  4. foreach goes over the rows, string.Join glues each row into a string, and Console.WriteLine prints it.

Each element of rows holds a reference to its row, like any array variable. That is why a whole row can be replaced: rows[1] = [7, 8, 9]; puts into the second element a reference to a new row of three numbers.

Here the picture of a wall of lockers is not quite right. The rows of a jagged array do not stand right next to each other. Each row is a separate array in its own place in memory, and the outer array holds only references to them.

Which array to choose

How a jagged array differs from a multidimensional one is a common interview question. The difference comes down to three points:

int[,]int[][]
Row lengththe sameeach row has its own
Element accessgrid[1, 2]rows[1][2]
Structureone arrayan array of references to row arrays

A rectangular array suits a table whose rows have the same length, such as a game board. A jagged one suits rows of different lengths, such as a grade book where each student has a different number of grades.

Common mistake

The number of rows of a rectangular array was taken from Length:

C#
int[,] grid = new int[2, 3];
for (int r = 0; r < grid.Length; r++) // ✗
{
    Console.WriteLine(grid[r, 0]);
}
TEXT
Unhandled exception. System.IndexOutOfRangeException: Index was outside the bounds of the array.

The program prints 0 twice and stops with the exception. For a rectangular array, Length is the number of all elements, 6, while there are only 2 rows, and when r is 2 there is no such row. The number of rows is grid.GetLength(0).

Key points

  • int[,] is a rectangular array: all rows have the same length, and an element is grid[row, column].
  • For int[,], Length is the number of all elements, while GetLength(0) and GetLength(1) are the numbers of rows and columns.
  • int[][] is a jagged array, an array of arrays: each row has its own length, and an element is rows[row][index].
  • int[,] is filled with values in curly braces, int[][] with values listed in square brackets: [[1, 2], [3]].