C# Jagged Array

Shahzad Aslam
4 min readMar 30, 2024

In C#, a jagged array is an array of arrays. Unlike rectangular (multidimensional) arrays, jagged arrays allow each element of the outer array to be an array of different lengths. This flexibility makes jagged arrays useful for representing irregular data structures where the inner arrays can have varying sizes. Each row (dimension) has a different length or size in jagged arrays, but in a multidimensional array, each row (dimension) has fixed or the same length because because each row is essentially a separate array.

C# Jagged Array

Let’s look it with Jagged array in C#

// Jagged array declaration
int[][] jaggedIntArray = new int[3][];
// Initialize jagged array
jaggedIntArray[0] = new int[] { 1, 2, 3 };
jaggedIntArray[1] = new int[] { 4, 5, 6, 7 };
jaggedIntArray[2] = new int[] { 8, 9 };
// Access elements
Console.WriteLine(jaggedIntArray[0][0]); // Output: 1
Console.WriteLine(jaggedIntArray[1][2]); // Output: 6
Console.WriteLine(jaggedIntArray[2][1]); // Output: 9

This example of a jagged array features rows of varying lengths, with the first row containing 3 elements, the second row containing 4 elements, and the third row containing 2 elements.

Now, let’s look at a multidimensional array example in C#:

// Jagged array declaration
int[][] jaggedIntArray = new…

--

--

Shahzad Aslam

Welcome to c-sharptutorial.com. I am Shahzad Aslam. I am the founder of c-sharptutorial.com. I'm currently living near Islamabad, Pakistan.