0Pricing
C# Academy · Lesson

Multidimensional Arrays

Grids and jagged arrays.

Multidimensional Arrays is a free C# Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Beyond a Single Row

So far arrays were a single line of values. A multidimensional array stores values in a grid, like a spreadsheet with rows and columns.

These are great for game boards, tables, and matrices.

Declaring a 2D Array

A two-dimensional array uses a comma inside the brackets: [,]. You give two sizes, the number of rows and the number of columns.

This grid has 2 rows and 3 columns, so it holds 6 values.

int[,] grid = new int[2, 3];
Console.WriteLine(grid[0, 0]); // 0

Accessing Cells

To read or write a cell, give both the row and the column index, separated by a comma.

Both indexes are zero-based, just like one-dimensional arrays.

int[,] grid = new int[2, 3];
grid[0, 1] = 7;
grid[1, 2] = 9;
Console.WriteLine(grid[1, 2]); // 9

Initializing a Grid

You can fill a 2D array at creation using nested braces. Each inner group is one row.

The rows must all have the same number of columns.

int[,] grid = { { 1, 2, 3 }, { 4, 5, 6 } };
Console.WriteLine(grid[1, 0]); // 4

The GetLength Method

With two dimensions, Length gives the total cell count. To get the size of one dimension, use GetLength.

GetLength(0) is the number of rows and GetLength(1) is the number of columns.

int[,] grid = { { 1, 2, 3 }, { 4, 5, 6 } };
Console.WriteLine(grid.Length);        // 6
Console.WriteLine(grid.GetLength(0));  // 2
Console.WriteLine(grid.GetLength(1));  // 3

Looping a Grid

To visit every cell, use one loop for rows inside another for columns. The outer loop picks a row, the inner loop walks its columns.

Use GetLength so the loops match the real size.

int[,] grid = { { 1, 2 }, { 3, 4 } };
for (int r = 0; r < grid.GetLength(0); r++)
{
    for (int c = 0; c < grid.GetLength(1); c++)
    {
        Console.WriteLine(grid[r, c]);
    }
}

A Full Grid Program

This complete program builds a small grid, then prints each value with its row and column.

Run it to see all four cells listed.

int[,] grid = { { 10, 20 }, { 30, 40 } };
for (int r = 0; r < grid.GetLength(0); r++)
{
    for (int c = 0; c < grid.GetLength(1); c++)
    {
        Console.WriteLine("[" + r + "," + c + "] = " + grid[r, c]);
    }
}

Three Dimensions

You can add more dimensions with more commas. A 3D array uses [,,] and three sizes.

Think of it as a stack of grids. They are rarer but follow the same rules.

int[,,] cube = new int[2, 2, 2];
cube[1, 1, 1] = 5;
Console.WriteLine(cube[1, 1, 1]); // 5

Jagged Arrays

A jagged array is an array of arrays, written [][]. Unlike a rectangular grid, each row can have a different length.

You access it with two separate bracket pairs.

int[][] jagged = new int[2][];
jagged[0] = new int[] { 1, 2 };
jagged[1] = new int[] { 3, 4, 5 };
Console.WriteLine(jagged[1][2]); // 5

Rectangular vs Jagged

A rectangular [,] array always has equal-length rows and stores data compactly. A jagged [][] array allows different row lengths.

Choose rectangular for true tables, jagged when rows naturally vary in size.

int[][] rows = { new[] { 1 }, new[] { 2, 3, 4 } };
Console.WriteLine(rows[0].Length); // 1
Console.WriteLine(rows[1].Length); // 3

Summing a Grid

You can total every value in a grid by adding each cell inside nested loops.

This pattern scales to any number of rows and columns.

int[,] grid = { { 1, 2 }, { 3, 4 } };
int total = 0;
foreach (int v in grid)
{
    total += v;
}
Console.WriteLine(total); // 10

Quick Check

Test your multidimensional array knowledge.

Recap

You learned to build grids with [,], access cells with row and column indexes, and measure them with GetLength.

Nested loops visit every cell, extra commas add dimensions, and jagged [][] arrays allow rows of different lengths. You can now store and process tables of data in C#.

Frequently asked questions

Is the “Multidimensional Arrays” lesson free?

Yes — the full text of “Multidimensional Arrays” is free to read here on the web, and the C# Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.

What will I learn in “Multidimensional Arrays”?

Grids and jagged arrays. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C# Academy?

No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Multidimensional Arrays” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C# Academy lesson?

Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Declaring Arrays
  2. Indexing and Length
  3. Looping Over Arrays
  4. Multidimensional Arrays
← Back to C# Academy