<style> .reveal-up, .reveal-left, .reveal-right, .reveal-stagger > * { opacity: 1 !important; transform: none !important; } </style>
  1. Learn
  2. C#
  3. Arrays and methods
All lessons in this course

Lesson 12 of 60 13 minBeginner

Multi-dimensional and jagged arrays

Store tables and grids with 2D arrays, rows of different lengths with jagged arrays, and loop through both.

A normal array is one row of values. Many things are tables: seats in a theatre, pixels in an image, a chess board, the floors and grid lines of a building. C# has two ways to store them.

Interactive

Rows and columns

Click any cell. Every row has the same number of columns.

grid[1, 2]= 7 · grid.GetLength(0) = 3 rows, grid.GetLength(1) = 4 columns, grid.Length = 12 cells

Rectangular (2D) arrays: int[,]

One comma inside the brackets means two dimensions: rows and columns. Every row has the same number of columns.

Program.cs
int[,] grid = new int[3, 4];     // 3 rows, 4 columns, all 0 int[,] seats ={    { 1, 2, 3, 4 },    { 5, 6, 7, 8 },    { 9, 10, 11, 12 }}; Console.WriteLine(seats[1, 2]);          // row 1, column 2 → 7seats[0, 0] = 100;Console.WriteLine(seats.GetLength(0));   // rows: 3Console.WriteLine(seats.GetLength(1));   // columns: 4Console.WriteLine(seats.Length);         // all cells: 12
Output
7
3
4
12

Looping through a 2D array

RowTotals.cs
int[,] sales ={    { 5, 3, 8 },    // shop 0: Mon, Tue, Wed    { 2, 7, 4 },    // shop 1}; for (int r = 0; r < sales.GetLength(0); r++){    int rowTotal = 0;    for (int c = 0; c < sales.GetLength(1); c++)    {        rowTotal += sales[r, c];    }    Console.WriteLine($"Shop {r}: {rowTotal}");}
Output
Shop 0: 16
Shop 1: 13

The outer loop walks the rows and the inner loop walks the columns of the current row. That is called row-major order; press “Nested loop order” in the diagram to watch it.

Jagged arrays: int[][]

A jagged array is an array of arrays. Each row is its own separate array, so rows can have different lengths, like floors of a building with different numbers of rooms.

Program.cs
int[][] rooms = new int[3][];   // 3 rows, not created yet (null)rooms[0] = new int[] { 101, 102 };rooms[1] = new int[] { 201, 202, 203, 204 };rooms[2] = new int[] { 301 }; Console.WriteLine(rooms[1][3]);       // 204 (note: [row][col])Console.WriteLine(rooms.Length);      // 3 rowsConsole.WriteLine(rooms[1].Length);   // 4 in row 1 foreach (int[] floor in rooms){    Console.WriteLine(string.Join(" ", floor));}
Output
204
3
4
101 102
201 202 203 204
301
2D array `int[,]`Jagged array `int[][]`
ShapePerfect rectangleEach row any length
Accessa[r, c]a[r][c]
Rows / columnsGetLength(0) / GetLength(1)a.Length / a[r].Length
CreatedAll at onceOuter array first, then each row
Typical useGrids, matrices, boardsUneven data, lines of text

A matrix example

In BIM and 3D software, positions and rotations are stored in matrices: 2D arrays of numbers. Here is a 3×3 identity matrix, which leaves points unchanged:

Identity.cs
int size = 3;double[,] identity = new double[size, size];for (int i = 0; i < size; i++)    identity[i, i] = 1;      // 1 on the diagonal, 0 elsewhere for (int r = 0; r < size; r++){    for (int c = 0; c < size; c++)        Console.Write(identity[r, c] + " ");    Console.WriteLine();}
Output
1 0 0 
0 1 0 
0 0 1 
Quick check

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

Quick check

How do you read row 2, column 1 of a jagged array j?

Practice

Create a 3×3 int[,] with the values 1 to 9 and print the sum of the main diagonal (top-left to bottom-right) and the sum of each column.

Show the solution
Solution.cs
int[,] m ={    { 1, 2, 3 },    { 4, 5, 6 },    { 7, 8, 9 }}; int diagonal = 0;for (int i = 0; i < 3; i++) diagonal += m[i, i];Console.WriteLine($"Diagonal: {diagonal}"); for (int c = 0; c < m.GetLength(1); c++){    int colSum = 0;    for (int r = 0; r < m.GetLength(0); r++) colSum += m[r, c];    Console.WriteLine($"Column {c}: {colSum}");}
Output
Diagonal: 15
Column 0: 12
Column 1: 15
Column 2: 18

Remember

  • int[,] is a rectangular grid accessed with a[row, col].
  • GetLength(0) = rows, GetLength(1) = columns, Length = all cells.
  • int[][] is an array of arrays; each row can be a different length.
  • Use nested loops: outer over rows, inner over columns.
0 of 60 lessons completed