TExcelTAD
Home › C# › C# Multidimensional Arrays

C# Multidimensional Arrays

A one-dimensional array is a row of values. Many problems, however, are naturally two-dimensional: a chessboard, a spreadsheet, a seating plan, the pixels of an image. C# supports two kinds of multi-level arrays: rectangular arrays and jagged arrays.

Rectangular arrays

A rectangular array has a fixed number of rows and columns. Declare it with a comma inside the brackets: int[,] is 2D, int[,,] is 3D. Access an element with [row, column].

csharp · try it yourself
using System;

int[,] matrix =
{
    { 1, 2, 3 },
    { 4, 5, 6 }
};

Console.WriteLine(matrix[0, 2]);
Console.WriteLine(matrix[1, 0]);

matrix[1, 1] = 50;
Console.WriteLine(matrix[1, 1]);

Console.WriteLine(
quot;Rank: {matrix.Rank}"); Console.WriteLine(
quot;Rows: {matrix.GetLength(0)}, Columns: {matrix.GetLength(1)}"); Console.WriteLine(
quot;Total elements: {matrix.Length}");
3
4
50
Rank: 2
Rows: 2, Columns: 3
Total elements: 6
  • Rank is the number of dimensions.
  • GetLength(d) returns the size of dimension d (0 = rows, 1 = columns).
  • Length is the total number of elements in all dimensions.

Looping over a 2D array

Use nested for loops with GetLength for the bounds. A foreach also works — it visits every element row by row — but it gives you no row/column numbers.

csharp · try it yourself
using System;

int[,] seats = new int[3, 4];   // 3 rows, 4 seats each, all 0 (free)
seats[0, 1] = 1;
seats[1, 3] = 1;
seats[2, 0] = 1;

for (int row = 0; row < seats.GetLength(0); row++)
{
    Console.Write(
quot;Row {row}: "); for (int col = 0; col < seats.GetLength(1); col++) { Console.Write(seats[row, col] == 1 ? "X " : ". "); } Console.WriteLine(); } int taken = 0; foreach (int s in seats) taken += s; Console.WriteLine(
quot;Taken seats: {taken}");
Row 0: . X . . 
Row 1: . . . X 
Row 2: X . . . 
Taken seats: 3

Example: adding two matrices

csharp · try it yourself
using System;

int[,] a = { { 1, 2 }, { 3, 4 } };
int[,] b = { { 10, 20 }, { 30, 40 } };
int[,] sum = new int[2, 2];

for (int r = 0; r < 2; r++)
    for (int c = 0; c < 2; c++)
        sum[r, c] = a[r, c] + b[r, c];

for (int r = 0; r < 2; r++)
    Console.WriteLine(
quot;{sum[r, 0],4}{sum[r, 1],4}");
  11  22
  33  44

Jagged arrays

A jagged array is an array of arrays. Each inner array is independent and can have its own length — useful when rows naturally differ in size, such as the number of lessons per week or the triangle of Pascal. It is declared with two pairs of brackets: int[][].

csharp · try it yourself
using System;

int[][] pascal = new int[5][];

for (int row = 0; row < pascal.Length; row++)
{
    pascal[row] = new int[row + 1];
    pascal[row][0] = 1;
    pascal[row][row] = 1;
    for (int i = 1; i < row; i++)
        pascal[row][i] = pascal[row - 1][i - 1] + pascal[row - 1][i];
}

foreach (int[] row in pascal)
    Console.WriteLine(string.Join(" ", row));

Console.WriteLine(
quot;Row 3 has {pascal[3].Length} numbers");
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Row 3 has 4 numbers

Notice the different access syntax: pascal[row][i] for jagged arrays versus matrix[row, col] for rectangular ones.

Rectangular or jagged?

Rectangular int[,] Jagged int[][]
Shape Every row has the same length Rows can differ
Access a[r, c] a[r][c]
Memory One contiguous block One array per row
Row as its own array Not possible a[r] is an int[]
Creation new int[3, 4] new int[3][], then each row

Tip: For grids with a fixed size (board games, images, matrices) choose a rectangular array. For "lists of lists" with varying lengths, choose a jagged array — or a List<List<T>> if the sizes change at run time.

Test yourself

How do you read row 1, column 2 of int[,] grid?

  1. grid[1][2]
  2. grid[1, 2]
  3. grid(1, 2)
  4. grid[2, 1]

For int[,] m = new int[4, 5];, what is m.Length?

  1. 4
  2. 5
  3. 9
  4. 20

Which declaration creates a jagged array?

  1. int[,] x;
  2. int[][] x;
  3. int[2] x;
  4. int x[][];