Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Types and Literals

C# jagged and multidimensional arrays

Reference for C# jagged arrays (int[][]) and rectangular arrays (int[,]): creating, indexing, row lengths, performance differences and when to use each.

C# offers two ways to build a table of values. A rectangular array such as int[,] is one block of memory with a fixed number of rows and columns. A jagged array such as int[][] is an array whose elements are themselves arrays, so each row is a separate object and may have its own length.

Both are indexed with two numbers, but the syntax differs: grid[row, col] for rectangular and jagged[row][col] for jagged. In practice jagged arrays are faster in .NET and more flexible; rectangular arrays are tidier when the shape is truly a fixed matrix.

C#
int[,] rect = new int[3, 4];      // 3 rows x 4 columns, one object
int[][] jagged = new int[3][];    // 3 rows, each still null

Try it

C# Jagged arrays have independent rows

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Rectangular arrays

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
T[,]  rect   = new T[rows, cols];
T[,]  rect2  = { { a, b }, { c, d } };
T[][] jagged = new T[rows][];             // then jagged[i] = new T[len];
T[][] jag2   = { new[] { a }, new[] { b, c } };
T[][,] mixed;                              // array of rectangular arrays
Property Rectangular T[,] Jagged T[][]
Memory One object One object for the outer array plus one per row
Row length Same for all rows Can differ per row
Index syntax a[r, c] a[r][c]
Length Total number of cells Number of rows
Rows as objects No Yes, can be passed or swapped (a[0] = a[1])
GetLength(i) Size of dimension i Not applicable (use a[r].Length)

Errors. Out-of-range indexes throw IndexOutOfRangeException. Using an uncreated row of a jagged array throws NullReferenceException.

Usage notes

  • Speed. Jagged arrays are usually faster in .NET: the JIT optimizes one-dimensional T[] accesses well, while multidimensional access goes through slower generated code. Benchmark before choosing for a hot loop.
  • Memory shape. A rectangular array wastes nothing, whereas a jagged array adds an object header and a reference per row, but it is the only choice for ragged data such as triangles or adjacency lists.
  • Initialization. new int[3][] creates three null rows. Forgetting to allocate each row is the most common jagged-array bug.
  • foreach. On int[,] it visits every element; on int[][] it visits rows, so you need a nested loop.
  • Copying. Clone() on a jagged array is shallow, so rows are shared with the original. Copy each row for a deep copy.
  • Flattening. Store a matrix in one T[] and compute index = row * width + col for the best cache behavior; wrap it in a small class for readable access.
  • Interfaces. int[,] does not implement IEnumerable<int>, only the non-generic IEnumerable, so LINQ needs Cast<int>().
  • Spans. Span<T> cannot wrap an int[,] without unsafe code; jagged rows can be wrapped as normal spans.