Multidimensional Arrays
C# supports arrays with more than one dimension. This lesson covers 2D arrays, rectangular arrays, and jagged arrays.
2D Arrays (Rectangular)
A 2D array is like a table with rows and columns:
csharp1// Declare a 3x4 array (3 rows, 4 columns)2int[,] matrix = new int[3, 4];34// Access elements with [row, column]5matrix[0, 0] = 1; // First row, first column6matrix[1, 2] = 5; // Second row, third column7matrix[2, 3] = 9; // Third row, fourth column
Initialize with Values
csharp1int[,] grid = {2 { 1, 2, 3 },3 { 4, 5, 6 },4 { 7, 8, 9 }5};67Console.WriteLine(grid[0, 0]); // 18Console.WriteLine(grid[1, 1]); // 59Console.WriteLine(grid[2, 2]); // 9
Getting Dimensions
csharp1int[,] matrix = new int[3, 4];23int rows = matrix.GetLength(0); // 34int columns = matrix.GetLength(1); // 45int total = matrix.Length; // 12 (total elements)
Iterating Over 2D Arrays
csharp1int[,] grid = {2 { 1, 2, 3 },3 { 4, 5, 6 },4 { 7, 8, 9 }5};67// Using nested for loops8for (int row = 0; row < grid.GetLength(0); row++)9{10 for (int col = 0; col < grid.GetLength(1); col++)11 {12 Console.Write($"{grid[row, col]} ");13 }14 Console.WriteLine();15}1617// Using foreach (iterates all elements, no index)18foreach (int value in grid)19{20 Console.Write($"{value} ");21}
Practical 2D Array Example
Multiplication Table
csharp1int size = 10;2int[,] table = new int[size, size];34// Fill the table5for (int i = 0; i < size; i++)6{7 for (int j = 0; j < size; j++)8 {9 table[i, j] = (i + 1) * (j + 1);10 }11}1213// Print the table14Console.WriteLine("Multiplication Table:");15Console.Write(" ");16for (int j = 1; j <= size; j++)17{18 Console.Write($"{j,4}");19}20Console.WriteLine();21Console.WriteLine(new string('-', 4 + size * 4));2223for (int i = 0; i < size; i++)24{25 Console.Write($"{i + 1,3}|");26 for (int j = 0; j < size; j++)27 {28 Console.Write($"{table[i, j],4}");29 }30 Console.WriteLine();31}
Tic-Tac-Toe Board
csharp1char[,] board = {2 { 'X', 'O', 'X' },3 { 'O', 'X', 'O' },4 { 'O', 'X', 'X' }5};67void PrintBoard()8{9 Console.WriteLine(" 0 1 2");10 for (int row = 0; row < 3; row++)11 {12 Console.Write($"{row} ");13 for (int col = 0; col < 3; col++)14 {15 Console.Write($" {board[row, col]} ");16 if (col < 2) Console.Write("|");17 }18 Console.WriteLine();19 if (row < 2) Console.WriteLine(" ---+---+---");20 }21}2223PrintBoard();
3D Arrays
For three-dimensional data:
csharp1// 2x3x4 array (like 2 tables of 3 rows × 4 columns)2int[,,] cube = new int[2, 3, 4];34cube[0, 0, 0] = 1;5cube[1, 2, 3] = 100;67// Iterate8for (int i = 0; i < cube.GetLength(0); i++)9{10 for (int j = 0; j < cube.GetLength(1); j++)11 {12 for (int k = 0; k < cube.GetLength(2); k++)13 {14 Console.Write($"[{i},{j},{k}]={cube[i,j,k]} ");15 }16 Console.WriteLine();17 }18 Console.WriteLine("---");19}
Jagged Arrays
Jagged arrays are "arrays of arrays" where each row can have different lengths:
csharp1// Declare jagged array2int[][] jagged = new int[3][];34// Initialize each row with different sizes5jagged[0] = new int[] { 1, 2 };6jagged[1] = new int[] { 3, 4, 5, 6 };7jagged[2] = new int[] { 7, 8, 9 };89// Access elements10Console.WriteLine(jagged[0][1]); // 211Console.WriteLine(jagged[1][3]); // 6
Initialize Inline
csharp1int[][] triangle = {2 new[] { 1 },3 new[] { 1, 1 },4 new[] { 1, 2, 1 },5 new[] { 1, 3, 3, 1 },6 new[] { 1, 4, 6, 4, 1 }7};89// Print Pascal's Triangle10foreach (int[] row in triangle)11{12 foreach (int num in row)13 {14 Console.Write($"{num} ");15 }16 Console.WriteLine();17}
Iterating Jagged Arrays
csharp1int[][] data = {2 new[] { 1, 2, 3 },3 new[] { 4, 5 },4 new[] { 6, 7, 8, 9 }5};67for (int i = 0; i < data.Length; i++)8{9 Console.Write($"Row {i}: ");10 for (int j = 0; j < data[i].Length; j++)11 {12 Console.Write($"{data[i][j]} ");13 }14 Console.WriteLine();15}
Rectangular vs Jagged
| Feature | Rectangular [,] | Jagged [][] |
|---|---|---|
| Memory layout | Contiguous | Separate arrays |
| Row sizes | Must be equal | Can vary |
| Syntax | arr[i, j] | arr[i][j] |
| Performance | Slightly faster | More flexible |
| Use case | Grids, matrices | Varying row lengths |
When to Use Each
csharp1// Rectangular: Fixed grids, game boards, matrices2int[,] chessBoard = new int[8, 8];3double[,] imagePixels = new double[1920, 1080];45// Jagged: Variable row lengths, nested data6string[][] menuItems = {7 new[] { "File", "Edit", "View" },8 new[] { "New", "Open", "Save", "Close" },9 new[] { "Cut", "Copy", "Paste" }10};
Practical Example: Seating Chart
csharp1// Different rows have different numbers of seats2string[][] theater = {3 new[] { "A1", "A2", "A3", "A4", "A5" },4 new[] { "B1", "B2", "B3", "B4", "B5", "B6" },5 new[] { "C1", "C2", "C3", "C4", "C5", "C6", "C7" },6 new[] { "D1", "D2", "D3", "D4", "D5", "D6", "D7", "D8" }7};89bool[][] occupied = {10 new[] { true, false, false, true, false },11 new[] { false, false, true, true, false, false },12 new[] { true, true, true, false, false, false, false },13 new[] { false, false, false, false, true, true, false, false }14};1516Console.WriteLine("Theater Seating (X = taken, O = available):");17Console.WriteLine();1819for (int row = 0; row < theater.Length; row++)20{21 Console.Write($"Row {(char)('A' + row)}: ");22 for (int seat = 0; seat < theater[row].Length; seat++)23 {24 char status = occupied[row][seat] ? 'X' : 'O';25 Console.Write($"[{status}] ");26 }27 Console.WriteLine();28}2930// Count available seats31int available = 0;32for (int row = 0; row < occupied.Length; row++)33{34 for (int seat = 0; seat < occupied[row].Length; seat++)35 {36 if (!occupied[row][seat])37 available++;38 }39}40Console.WriteLine($"\nAvailable seats: {available}");
Summary
In this lesson, you learned:
- 2D rectangular arrays use
[,]syntax with fixed dimensions - Access elements with
array[row, column] - Use
GetLength(dimension)to get dimension sizes - Jagged arrays
[][]allow varying row lengths - Choose rectangular for fixed grids, jagged for variable structures
- Nested loops iterate over multidimensional arrays
Next, we'll explore the List