1. Introduction
Imagine a regular array as a long pizza box, where all the slices are lined up in a row — tasty, but kinda boring.
Now picture a work schedule or a chessboard. There, everything's organized into rows and columns — basically, a table. That's the classic use case for a two-dimensional array.
In real life, two-dimensional arrays pop up everywhere:
- Employee salary tables (rows — employees, columns — months).
- A picture on the screen (each "cell" is a pixel color, with two coordinates: X and Y).
- Matrix calculations and data processing.
- Chessboard or tic-tac-toe field.
Sometimes a two-dimensional array is called a matrix (that name comes from math).
There are tons of places where, as a programmer, you'll need a two-dimensional array. Pretty much any board game implementation is a ready-made two-dimensional array: "Chess", "Checkers", "Tic-Tac-Toe", "Battleship":
The game board for "Chess" or "Battleship" is just perfect for two-dimensional arrays, where you use numbers as cell coordinates. Not "pawn e2 → e4", but "pawn (4,1) → (4,3)". It's actually easier for you as a coder.
2. Syntax for Declaring Two-Dimensional Arrays
It's only scary until your first declaration! Let's break it down step by step.
General rule
type[,] arrayName;
That comma , inside the brackets — it's not a compiler bug after a night with too much coffee, it's telling you: the array is two-dimensional.
Examples
int[,] matrix;
double[,] gradesTable;
string[,] chessBoard;
Creating an Array
Set the sizes: number of rows and columns.
matrix = new int[3, 4]; // 3 rows, 4 columns
This is a 3x4 table: imagine an Excel sheet with 3 rows and 4 columns.
You can declare the variable and create the two-dimensional array right away:
int[,] matrix = new int[3, 4];
You can also initialize it with values right away (kinda like with a one-dimensional array):
int[,] example = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// Table: 3 rows, 3 columns
Important: ALL rows must be the same length when initializing like this. You can't make the first row 3 elements, the second — 2.
Visual diagram
┌─────┬─────┬─────┬─────┐
│ 0 │ 1 │ 2 │ 3 │ ← columns
├─────┼─────┼─────┼─────┤
│ 0,0 │0,1 │0,2 │0,3 │ ← row 0
├─────┼─────┼─────┼─────┤
│ 1,0 │1,1 │1,2 │1,3 │ ← row 1
├─────┼─────┼─────┼─────┤
│ 2,0 │2,1 │2,2 │2,3 │ ← row 2
└─────┴─────┴─────┴─────┘
Each element is set by a pair of indices [row, column].
3. Indexing and Accessing Elements
In a two-dimensional array, you use two indices to access an element:
- First — row number
- Second — column number
matrix[1, 2] = 99; // In the second row (index 1), third column (index 2)
If you're used to chess, just a reminder: indexing starts at zero. So, the first element is [0, 0].
Writing and reading
int[,] data = new int[5, 2];
data[1, 1] = 5; // writing
int value = data[1, 1]; // reading
Here's what it looks like in memory:
4. Filling a Two-Dimensional Array
Let's write some code that fills a matrix with consecutive numbers from 1 to 12, so you can see how it works.
int[,] matrix = new int[3, 4];
int value = 1;
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 4; col++)
{
matrix[row, col] = value;
value++;
}
}
Here, the outer loop goes through rows, the inner one — columns.
Illustration:
After running:
┌────┬────┬────┬────┐
│ 1 │ 2 │ 3 │ 4 │
├────┼────┼────┼────┤
│ 5 │ 6 │ 7 │ 8 │
├────┼────┼────┼────┤
│ 9 │10 │11 │12 │
└────┴────┴────┴────┘
5. Dimensions: Getting the Number of Rows and Columns
You often need to get the array's size "on the fly". In C#, there's a handy method for that: GetLength().
int[,] matrix = new int[3, 4];
int rows = matrix.GetLength(0); // number of rows (first dimension) - 3
int columns = matrix.GetLength(1); // number of columns (second dimension) - 4
Console.WriteLine($"Rows: {rows}, Columns: {columns}");
don't mix it up with Length — for a two-dimensional array, that's the total number of all elements (rows × columns).
6. Printing a Two-Dimensional Array
To print a two-dimensional table, you usually use a double loop — it's kinda like drawing a maze in the console. Example:
for (int row = 0; row < matrix.GetLength(0); row++)
{
for (int col = 0; col < matrix.GetLength(1); col++)
{
Console.Write(matrix[row, col] + "\t"); // "\t" — tab for nice formatting
}
Console.WriteLine(); // move to the next line
}
Sample output:
1 2 3 4
5 6 7 8
9 10 11 12
By the way, now you know how to "draw" tables nicely with just a couple lines of code!
7. Multidimensional Arrays
And here's another fun fact about arrays you might've guessed already. If you can make a two-dimensional array, can you make a three-dimensional one?
Yep, you can create arrays of any dimension. These are called multidimensional arrays.
How to Declare Multidimensional Arrays
Just list as many dimensions as you need, separated by commas:
int[,,,] matrix = new int[2, 3, 4, 5];
Here we've got a four-dimensional array:
- 2 elements by the first coordinate,
- 3 — by the second,
- 4 — by the third,
- 5 — by the fourth.
In memory, this kind of array is one big “cube” of data, packed in sequence.
How to Access Elements
You access an element by specifying all the indices at once:
matrix[0, 1, 2, 3] = 42;
int value = matrix[1, 2, 0, 4];
- Indices start at zero, as always in C#.
- There will be 2 × 3 × 4 × 5 = 120 elements in this array.
Practical Examples of Multidimensional Arrays
- 2D — tables, chessboards, images.
- 3D — “cubes” in computer graphics, data for scientific calculations (like temperature at different points in space and time).
- 4D and up — rarely used, but you might see them in advanced math, simulations, machine learning, etc.
GO TO FULL VERSION