C# › Arrays, Span<T> & Memory<T>

How do you create and work with a multidimensional (rectangular) array versus a jagged array?

Beginner

Answer

A rectangular array (int[,]) has a fixed grid shape where every row has the same length; a jagged array (int[][]) is an array of arrays, where each inner array can have a different length, offering more flexibility at the cost of an extra indirection.
int[,] grid = new int[3, 3]; // rectangular: fixed 3x3
grid[0, 0] = 1;

int[][] jagged = new int[3][];
jagged[0] = new int[] { 1, 2 };
jagged[1] = new int[] { 1, 2, 3 };
Real-world example Using a rectangular array for a fixed game board, versus a jagged array for storing rows of variable-length CSV data.

Common follow-ups: Which performs better in tight loops — rectangular or jagged arrays?