C# Multidimensional Arrays

A regular array stores items in a single line — like a row of lockers. A multidimensional array stores items in a grid or table — like a spreadsheet with rows and columns. C# supports both rectangular arrays and jagged arrays.

What Is a 2D Array?

A 2D (two-dimensional) array organizes data in rows and columns. Every row has the same number of columns — the grid is always rectangular.

┌────────────────────────────────────────────────────────────┐
│              2D ARRAY — GRID VISUALIZATION                 │
├────────────────────────────────────────────────────────────┤
│                                                            │
│              Column 0  Column 1  Column 2                  │
│   Row 0  →  [  10   ]  [  20  ]  [  30  ]                  │
│   Row 1  →  [  40   ]  [  50  ]  [  60  ]                  │
│   Row 2  →  [  70   ]  [  80  ]  [  90  ]                  │
│                                                            │
│   Access: array[row, column]                               │
│   array[1, 2] = 60   (Row 1, Column 2)                     │
└────────────────────────────────────────────────────────────┘

Declaring a 2D Array

// Declare a 3x3 int array (3 rows, 3 columns):
int[,] grid = new int[3, 3];

// Declare and initialize at the same time:
int[,] matrix = {
    { 10, 20, 30 },
    { 40, 50, 60 },
    { 70, 80, 90 }
};

Accessing Elements

Use two indices: [row, column]. Both start at 0.

int[,] matrix = {
    { 10, 20, 30 },
    { 40, 50, 60 },
    { 70, 80, 90 }
};

Console.WriteLine(matrix[0, 0]);  // 10  (Row 0, Col 0)
Console.WriteLine(matrix[1, 2]);  // 60  (Row 1, Col 2)
Console.WriteLine(matrix[2, 1]);  // 80  (Row 2, Col 1)

Looping Through a 2D Array

Use nested for loops — one for rows, one for columns.

int[,] scores = {
    { 85, 90, 78 },
    { 60, 72, 88 },
    { 95, 66, 71 }
};

int rows = scores.GetLength(0);    // number of rows = 3
int cols = scores.GetLength(1);    // number of columns = 3

for (int r = 0; r < rows; r++)
{
    for (int c = 0; c < cols; c++)
    {
        Console.Write(scores[r, c] + "\t");
    }
    Console.WriteLine();
}
// Output:
// 85   90   78
// 60   72   88
// 95   66   71

Real Example: Seating Chart

┌─────────────────────────────────────────────────────────────┐
│            CINEMA SEATING CHART                             │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  string[,] seats = new string[3, 4];                        │
│                                                             │
│           Col0     Col1     Col2     Col3                   │
│  Row0  [ A1    ] [ A2    ] [ A3    ] [ A4    ]              │
│  Row1  [ B1    ] [ B2    ] [ B3    ] [ B4    ]              │
│  Row2  [ C1    ] [ C2    ] [ C3    ] [ C4    ]              │
│                                                             │
│  seats[0, 0] = "A1"   seats[1, 2] = "B3"                    │
└─────────────────────────────────────────────────────────────┘
string[,] seats = new string[3, 4];

for (int r = 0; r < 3; r++)
{
    for (int c = 0; c < 4; c++)
    {
        char row = (char)('A' + r);
        seats[r, c] = row + (c + 1).ToString();
    }
}

Console.WriteLine(seats[0, 0]);   // A1
Console.WriteLine(seats[2, 3]);   // D4

3D Arrays

A 3D array adds a third dimension — think of it as multiple grids stacked on top of each other. A good example is a building: floors (z) × rows (x) × seats (y).

┌─────────────────────────────────────────────────────────────┐
│                  3D ARRAY — BUILDING MODEL                  │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│   Floor 2: [ ][ ][ ]    ← array[2, row, col]                │
│   Floor 1: [ ][ ][ ]    ← array[1, row, col]                │
│   Floor 0: [ ][ ][ ]    ← array[0, row, col]                │
│                                                             │
└─────────────────────────────────────────────────────────────┘

int[,,] building = new int[3, 4, 5];
// 3 floors, 4 rows per floor, 5 seats per row

building[0, 1, 2] = 1;   // Floor 0, Row 1, Seat 2 is occupied

Jagged Arrays

A jagged array is an array of arrays — each row can have a different number of columns. Unlike rectangular 2D arrays, jagged arrays are not required to be the same size on every row.

┌─────────────────────────────────────────────────────────────┐
│               JAGGED ARRAY vs 2D ARRAY                      │
├──────────────────────────┬──────────────────────────────────┤
│ 2D Array (rectangular)   │ Jagged Array                     │
├──────────────────────────┼──────────────────────────────────┤
│ Row 0: [A][B][C]         │ Row 0: [A][B]                    │
│ Row 1: [D][E][F]         │ Row 1: [C][D][E][F]              │
│ Row 2: [G][H][I]         │ Row 2: [G]                       │
│                          │                                  │
│ All rows same length     │ Each row can differ in length    │
└──────────────────────────┴──────────────────────────────────┘

Declaring and Using Jagged Arrays

// Declare a jagged array with 3 rows
int[][] jagged = new int[3][];

// Each row gets its own size
jagged[0] = new int[] { 1, 2 };
jagged[1] = new int[] { 3, 4, 5, 6 };
jagged[2] = new int[] { 7 };

// Access: array[row][column]
Console.WriteLine(jagged[1][2]);   // 5

// Loop through jagged array
for (int r = 0; r < jagged.Length; r++)
{
    for (int c = 0; c < jagged[r].Length; c++)
    {
        Console.Write(jagged[r][c] + " ");
    }
    Console.WriteLine();
}
// Output:
// 1 2
// 3 4 5 6
// 7

2D vs Jagged Array: When to Use Which

┌────────────────────────┬──────────────────────────────────────┐
│ Use 2D Array when...   │ Use Jagged Array when...             │
├────────────────────────┼──────────────────────────────────────┤
│ Data is grid-shaped    │ Rows have different lengths          │
│ (matrix, table, map)   │ (triangle data, calendar rows)       │
├────────────────────────┼──────────────────────────────────────┤
│ All rows same length   │ Memory efficiency matters            │
├────────────────────────┼──────────────────────────────────────┤
│ Simpler access syntax  │ More flexible structure              │
│ array[r, c]            │ array[r][c]                          │
└────────────────────────┴──────────────────────────────────────┘

Useful Array Methods for 2D

int[,] data = new int[4, 5];

Console.WriteLine(data.GetLength(0));   // 4 — number of rows
Console.WriteLine(data.GetLength(1));   // 5 — number of columns
Console.WriteLine(data.Length);         // 20 — total elements
Console.WriteLine(data.Rank);           // 2 — number of dimensions

Quick Summary

┌──────────────────────────────────────────────────────────────┐
│ int[,]   → 2D rectangular array   → array[row, col]          │
│ int[,,]  → 3D array               → array[x, y, z]           │
│ int[][]  → jagged array           → array[row][col]          │
│                                                              │
│ GetLength(0) → rows                                          │
│ GetLength(1) → columns                                       │
│ Rank → number of dimensions                                  │
└──────────────────────────────────────────────────────────────┘

Multidimensional arrays model real-world grid problems perfectly — from game boards and maps to spreadsheets and image pixels. Mastering them opens the door to many practical and interesting programming challenges.

Leave a Comment

Your email address will not be published. Required fields are marked *