Types of Arrays in C#: Single-dimensional, Multi-dimensional and Jagged Array
Explore the different types of arrays in C#, including single-dimensional, multi-dimensional and jagged arrays.
Single-dimensional Arrays
A single-dimensional array is the simplest form, representing a straight line of elements accessed with just one index.
Multi-dimensional Arrays
A multi-dimensional array, such as a two-dimensional array, organizes data into rows and columns, making it ideal for representing grids or tables.
Jagged Arrays
A jagged array is an array of arrays, where each inner array can have a different length, offering more flexibility than a standard multi-dimensional array.
Choosing the Right Type
Single-dimensional arrays suit simple lists, multi-dimensional arrays suit uniform grid-like data, and jagged arrays are best when the size of each row varies.
Declaring and Using Each Type in Code
Single-dimensional
int[] marks = { 72, 85, 90 };
string[] cities = new string[3]; // three nulls by default
cities[0] = "Pune";
Console.WriteLine(marks.Length); // 3
foreach (int m in marks)
Console.WriteLine(m);
Multi-dimensional (rectangular)
int[,] seats = new int[2, 3]
{
{ 1, 0, 1 },
{ 0, 0, 1 }
};
for (int row = 0; row < seats.GetLength(0); row++)
{
for (int col = 0; col < seats.GetLength(1); col++)
Console.Write(seats[row, col] + " ");
Console.WriteLine();
}
Console.WriteLine(seats.Rank); // 2 dimensions
Console.WriteLine(seats.Length); // 6 elements in total
Jagged
int[][] scores = new int[3][];
scores[0] = new int[] { 10, 20 };
scores[1] = new int[] { 5 };
scores[2] = new int[] { 7, 8, 9, 6 };
foreach (int[] team in scores)
Console.WriteLine(string.Join(", ", team));
Notice the syntax difference: a rectangular array uses one pair of brackets with a comma, [,], and is indexed as seats[row, col]. A jagged array uses two pairs, [][], and is indexed as scores[row][col].
How They Differ in Memory
A rectangular array is stored as one block, with every row the same length. GetLength(0) returns the number of rows and GetLength(1) the number of columns. A jagged array is an outer array that holds references to separate inner arrays. Each inner array is created on its own and can be any length, or even null.
| Feature | Rectangular [,] | Jagged [][] |
|---|---|---|
| Row lengths | All equal | Can differ |
| Creation | One statement | Outer array, then each row |
| Length property | Total elements | Number of rows only |
| Works with LINQ per row | Not directly | Yes, each row is a normal array |
A good rule: use a rectangular array when the data is a true grid, such as a chessboard, a seating plan or a matrix in a calculation. Use a jagged array when rows naturally vary, such as marks per student where each student took a different number of tests, or lines of text with different word counts.
In .NET, jagged arrays are often slightly faster to access than rectangular ones, so performance-sensitive code sometimes prefers them even for uniform data.
Helpful Methods from the Array Class
int[] nums = { 40, 10, 30, 20 };
Array.Sort(nums); // 10, 20, 30, 40
int pos = Array.IndexOf(nums, 30); // 2
Array.Reverse(nums); // 40, 30, 20, 10
Array.Resize(ref nums, 6); // new array, two extra zeros
int[] copy = (int[])nums.Clone();
Arrays are reference types, so assigning one array variable to another does not copy the data; both variables point to the same elements. Use Clone() or Array.Copy when you need an independent copy.
Array.Resize does not grow the array in place; it creates a new one and copies the elements. If you resize often, a List<T> is the better choice.
Errors Beginners Run Into
- IndexOutOfRangeException: indexes start at 0, so the last element is
Length - 1. Use<, not<=, in loop conditions. - NullReferenceException with jagged arrays: creating
new int[3][]does not create the rows; each must be assigned before use. - Using Length on a 2D array as the row count: it returns the total number of elements; use
GetLength(0). - Choosing an array when the size changes: arrays have a fixed length; collections such as
List<T>grow automatically.