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.
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
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.
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
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 threenullrows. Forgetting to allocate each row is the most common jagged-array bug. foreach. Onint[,]it visits every element; onint[][]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 computeindex = row * width + colfor the best cache behavior; wrap it in a small class for readable access. - Interfaces.
int[,]does not implementIEnumerable<int>, only the non-genericIEnumerable, so LINQ needsCast<int>(). - Spans.
Span<T>cannot wrap anint[,]without unsafe code; jagged rows can be wrapped as normal spans.
Related
- Arrays,
foreach,new, Tuples - Lessons: Arrays, Collection interfaces, Spans and memory