Single Dimensional, Multi-Dimensional & Jagged Arrays in C#
Introduction to Arrays in C#
An **array** is a collection of elements of the **same data type**, stored in **contiguous memory locations**. Arrays in C# can be **single-dimensional, multi-dimensional, or jagged**.
Types of Arrays in C#:
- Single-Dimensional Array: Stores elements in a **linear** sequence.
- Multi-Dimensional Array: Stores elements in **rows and columns** (like a matrix).
- Jagged Array: An **array of arrays**, where each array can have different lengths.
Single Dimensional Arrays
A **single-dimensional array** is the simplest form of an array. Each element is accessed using a **single index**, starting from `0`.
Example: Single-Dimensional Array
int[] numbers = { 1, 2, 3, 4, 5 };
foreach (int num in numbers)
{
Console.WriteLine(num);
}
In this example, the **array numbers
stores a list of integers**.
The `foreach` loop iterates through the array and prints each element.
Multi-Dimensional Arrays
A **multi-dimensional array** (also called a **rectangular array**) stores data in a **tabular format** (rows and columns). You access elements using **multiple indices**.
Example: Multi-Dimensional Array (Matrix)
int[,] matrix = new int[3, 3]
{
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
for (int i = 0; i < matrix.GetLength(0); i++)
{
for (int j = 0; j < matrix.GetLength(1); j++)
{
Console.Write(matrix[i, j] + " ");
}
Console.WriteLine();
}
The **matrix** stores elements in **rows and columns**. `GetLength(0)` returns the **number of rows**, and `GetLength(1)` returns the **number of columns**.
Jagged Arrays
A **jagged array** is an **array of arrays**, where **each inner array can have different lengths**. Unlike **multi-dimensional arrays**, jagged arrays **do not require uniform row lengths**.
Example: Jagged Array
int[][] jaggedArray = new int[3][]
{
new int[] {1, 2},
new int[] {3, 4, 5},
new int[] {6, 7, 8, 9}
};
for (int i = 0; i < jaggedArray.Length; i++)
{
for (int j = 0; j < jaggedArray[i].Length; j++)
{
Console.Write(jaggedArray[i][j] + " ");
}
Console.WriteLine();
}
Here, each **row has a different number of elements**, making it **flexible** compared to rectangular arrays.
Memory Comparison: Multi-Dimensional vs Jagged Arrays
Feature | Multi-Dimensional Array | Jagged Array |
---|---|---|
Memory Allocation | Fixed block allocation (all rows have the same length). | Each row is allocated separately (more flexible). |
Access Performance | Faster as elements are stored in contiguous memory. | Slightly slower due to separate memory allocations. |
Flexibility | Fixed structure (all rows have the same length). | More flexible (each row can have a different length). |
Best Practices for Using Arrays
- Use Single-Dimensional Arrays: When data is **linear** and **fixed in size**.
- Use Multi-Dimensional Arrays: When **data needs structured row-column organization** (like grids).
- Use Jagged Arrays: When **rows need different lengths**, such as storing **variable-sized data lists**.
- Avoid Large Multi-Dimensional Arrays: If performance is critical, consider **collections (List<T>)** instead.
- Use `Length` or `GetLength(n)`:** To iterate arrays correctly and **avoid index errors**.