Multidimensional Arrays — The City Grid
15 XPMedium
Ctrl+Enter RunCtrl+S Save
🏙️ Mapping the City — Multidimensional Arrays
A single-dimensional array is like a shelf in a warehouse — great for a linear collection. But what if the architect needs to model something with rows and columns, like a city grid layout, a floor plan, or a spreadsheet? That's where multidimensional arrays come in.
C# offers two flavors: rectangular (2D) arrays and jagged arrays. They look similar but differ fundamentally in memory layout and flexibility.
🗺️ 2D Rectangular Arrays
A rectangular 2D array is declared with int[,]. Think of it as a perfect grid where every row has the same number of columns — like a city block map where each row represents a street and each column represents a building lot.
Unlike single-dimensional arrays where Length gives you the total element count, 2D arrays use GetLength(dimension) to get the size of a specific dimension. Dimension 0 is rows, dimension 1 is columns.
string[,] grid = {
{ "R", "C", "R" },
{ "P", "R", "P" },
{ "C", "R", "C" }
};
int rows = grid.GetLength(0); // 3
int cols = grid.GetLength(1); // 3
int total = grid.Length; // 9 (3 x 3)
// Loop through rows and columns
for (int r = 0; r < rows; r++)
{
for (int c = 0; c < cols; c++)
{
Console.Write($"[{grid[r, c]}]");
}
Console.WriteLine();
}
🏢 Jagged Arrays — Floors of Different Sizes
A jagged array is an "array of arrays" — each inner array can have a different length. Think of a building where each floor has a different number of rooms. Declared with int[][], each row is an independent array allocated separately on the heap.
// A building with 3 floors, each having different room counts
int[][] building = new int[3][];
building[0] = new int[] { 101, 102, 103 }; // Floor 0: 3 rooms
building[1] = new int[] { 201, 202, 203, 204, 205 }; // Floor 1: 5 rooms
building[2] = new int[] { 301, 302 }; // Floor 2: 2 rooms
// Access: building[floor][room]
Console.WriteLine(building[1][3]); // 204
// Each floor's length
for (int f = 0; f < building.Length; f++)
{
Console.WriteLine($"Floor {f}: {building[f].Length} rooms");
}
⚔️ Rectangular vs. Jagged — Key Differences
Memory layout: Rectangular arrays are a single contiguous block; jagged arrays are arrays of references to separate arrays.
Performance: The CLR optimizes jagged array access better in some cases (each inner array gets its own bounds check, enabling JIT optimizations). Rectangular arrays use a single call instruction per access.
Flexibility: Jagged arrays allow rows of different lengths; rectangular arrays enforce uniform dimensions.
Syntax: Rectangular uses [,] and GetLength(); jagged uses [][] and .Length on each inner array.
LINQ support: Jagged arrays work natively with LINQ since each row is a standard array. Rectangular arrays need casting via Cast<T>().
🔧 Useful Array Class Methods for Multi-Dim
// Rank — number of dimensions
string[,] grid = new string[3, 4];
Console.WriteLine(grid.Rank); // 2
// GetLowerBound / GetUpperBound
Console.WriteLine(grid.GetLowerBound(0)); // 0
Console.WriteLine(grid.GetUpperBound(0)); // 2
Console.WriteLine(grid.GetUpperBound(1)); // 3
// 3D array example
int[,,] cube = new int[2, 3, 4];
Console.WriteLine(cube.Rank); // 3
Console.WriteLine(cube.Length); // 24
⚡ Key Takeaways
Use int[,] for rectangular grids where every row has the same number of columns.
Use int[][] for jagged structures where rows can differ in length.
GetLength(n) returns the size of dimension n in a rectangular array.
Jagged arrays are arrays of arrays — each inner array is a separate object on the heap.
The Rank property tells you how many dimensions an array has.
📋 Instructions
## 🏙️ City Grid Layout Challenge
The architect is planning a new city district. You need to model it using both a 2D array and a jagged array.
1. Create a 2D `string` array called `cityGrid` (3 rows × 3 columns) with this layout:
- Row 0: `"R"`, `"C"`, `"R"`
- Row 1: `"P"`, `"R"`, `"P"`
- Row 2: `"C"`, `"R"`, `"C"`
2. Print `"City Grid:"` then loop through the grid, printing each row on one line with each cell wrapped in brackets `[X]` (no spaces between cells)
3. Create a jagged `int[][]` array called `floors` with 3 inner arrays of lengths **3**, **5**, and **2** (values don't matter)
4. Print the lengths of each inner array on one line: `"Jagged floors: 3, 5, 2"`
Initialize the 2D array with `string[,] cityGrid = { {"R","C","R"}, {"P","R","P"}, {"C","R","C"} };`. Use nested `for` loops with `GetLength(0)` for rows and `GetLength(1)` for columns, printing `$"[{cityGrid[r,c]}]"` with `Console.Write`. For the jagged array, create `new int[3][]` and assign each floor separately, then print `$"Jagged floors: {floors[0].Length}, {floors[1].Length}, {floors[2].Length}"`.