Jagged Arrays in C#
A jagged array is an array of arrays, where each element is itself an array that can have a different length. Rows are fixed at declaration, but columns can vary. It can also combine with multidimensional arrays.
Example: Declaring a jagged array with 3 rows
int[][] jaggedArr = new int[3][];
// Initialize each row with different lengths
jaggedArr[0] = new int[] { 1, 2, 3 };
jaggedArr[1] = new int[] { 4, 5 };
jaggedArr[2] = new int[] { 6, 7, 8, 9 };
Here's a visual representation how Jagged Arrays in C# are stored in Heap Memory:

Declaration and Initialization
For declaring we specify the type of elements inside the inner arrays and the number of rows in the outer array. Unlike multi-dimensional arrays, where the size of all dimensions is fixed, a jagged array allows for variable lengths of inner arrays.
Syntax
data_type[ ][ ] name_of_array = new data_type[rows][ ]
Initialization of Jagged Arrays
Method 1: Initialized with indexes.
// Add in the first row
jagged_arr[0][0] = 1;
jagged_arr[0][1] = 2;
Method 2: We can directly insert the arrays in each row of the jagged array in this way.
// Each row having different size of array as a element
jagged_arr[0] = new int[] {1, 2, 3, 4};
jagged_arr[1] = new int[] {11, 34, 67};
Declaration with Initialization
There are two methods to perform these operations one is
Method 1: Using the Direct Method
int[][] jagged_arr = new int[][] {
new int[] {1, 2, 3, 4},
new int[] {11, 34, 67}
};
Method 2: Using Short-hand Method.
There is no default initialization for the elements so a user cannot omit the new operator from the element's initialization.
int[][] jagged_arr = {
new int[] {1, 2, 3, 4},
new int[] {11, 34, 67}
};
Example : Basic program to implement jagged array
using System;
class Geeks{
static void Main(){
// Declaration
int[][] jaggedArray = new int[3][];
// Initialization
jaggedArray[0] = new int[] { 1, 2 };
jaggedArray[1] = new int[] { 3, 4, 5 };
jaggedArray[2] = new int[] { 6, 7, 8, 9 };
// Iterating the elements
for (int i = 0; i < jaggedArray.Length; i++)
{
Console.Write("Row " + i + ": ");
foreach(int num in jaggedArray[i])
Console.Write(num + " ");
Console.WriteLine();
}
}
}
Output
Row 0: 1 2 Row 1: 3 4 5 Row 2: 6 7 8 9
Accessing and Modifying Elements
We can access elements in a jagged array using two indices. The first index is from the outer array and the second index corresponds to the element in the row of the inner array
Example: In this example, we learn how to access and modify elements of a jagged array
using System;
class Geeks{
static void Main(string[] args){
// Declaration of a jagged array with 3 rows
int[][] arr = new int[3][];
// Initializing each row of the jagged array First row
arr[0] = new int[] { 1, 2, 3, 4};
// Second row
arr[1] = new int[] { 4, 5, 6 };
// Third row (only two elements)
arr[2] = new int[] { 7, 8 };
// Accessing the third element in second row
int value = arr[1][2];
// Modifying the second element in the first row (from 2 to 10)
arr[0][1] = 10;
// Outputting the modified value
Console.WriteLine(arr[0][1]);
// Outputting the accessed value for verification
Console.WriteLine("Accessed value: " + value);
}
}
Output
10 Accessed value: 6
Iterating in Multidimensional Array
To iterate through a jagged array, we can use nested loops. The outer loop iterates through the rows and the inner loop iterates through the elements in each row.
Example:
using System;
public class Geeks
{
public static void Main(string[] args)
{
// Declaration and Initialization with Multidimensional Array
int[][,] arr = new int[2][,] {
new int[, ] { { 1, 3 }, { 5, 7 } },
new int[, ] { { 0, 2 }, { 4, 6 }, { 8, 10 } }
};
// Display the array elements:
for (int i = 0; i < arr.Length; i++){
for (int j = 0; j < arr[i].GetLength(0); j++){
for (int k = 0; k < arr[i].GetLength(1); k++){
Console.Write("arr[" + i + "][" + j + ", " + k + "] => " + arr[i][j, k] + " ");
}
Console.WriteLine();
}
Console.WriteLine();
}
}
}
Output
arr[0][0, 0] => 1 arr[0][0, 1] => 3 arr[0][1, 0] => 5 arr[0][1, 1] => 7 arr[1][0, 0] => 0 arr[1][0, 1] => 2 arr[1][1, 0] => 4 arr[1][1, 1] => 6 arr[1][2, 0] => 8 arr[1][2, 1] => 10
Advantages of Jagged Arrays
- Flexibility: It allows us to define the variable number of arrays inside each row of an array.
- Dynamic Size: Allow us to add different array sizes in the run-time.
- Enhance Readability: It makes code more readable and easy to understand at first look.
- Memory efficient: Allow each inner array with a different size, they can be more memory-efficient when storing irregular data.
Which of the following is true about jagged arrays in C#?
-
A
All rows must have the same number of elements.
-
B
Each row can have a different number of elements.
-
C
Jagged arrays are also called rectangular arrays.
-
D
Jagged arrays cannot store primitive data types.
Jagged arrays are arrays of arrays, and each inner array can have a different length.
What is the key difference between a multidimensional array and a jagged array in C#?
-
A
Multidimensional arrays allow irregular rows, jagged arrays do not.
-
B
Jagged arrays allow irregular rows, multidimensional arrays do not.
-
C
Both are identical in structure.
-
D
Jagged arrays cannot store different types of elements.
Jagged arrays are arrays of arrays, where inner arrays may have different lengths, unlike multidimensional arrays which are rectangular in structure.
Which of the following correctly initializes a jagged array in C#?
-
A
int[,] arr = new int[3][4];
-
B
int[][] arr = new int[3][];
-
C
int arr[3][4];
-
D
int[][] arr = new int[][3];
Jagged arrays require separate initialization for each inner array.
What is a Jagged Array in C#?
-
A
An array where all rows must have equal length
-
B
A multidimensional array with fixed dimensions
-
C
An array of arrays where each inner array can have different lengths
-
D
A two-dimensional array stored in stack memory