csharp / intermediate
Snippet
Manipulating Jagged Array Structures
Jagged arrays are arrays of arrays where each internal array can have a different size, offering more flexibility than multidimensional rectangular arrays.
snippet.cs
csharp
1
2
3
4
5
6
7
8
9
10
11
12
13
int[][] jagged = new int[3][];jagged[0] = new int[] { 1, 2 };jagged[1] = new int[] { 3, 4, 5, 6 };jagged[2] = new int[] { 7 };foreach (var row in jagged){Console.WriteLine($"Row length: {row.Length}");foreach (var item in row){Console.Write($"{item} ");}}
Breakdown
1
int[][] jagged = new int[3][];
Initializes a top-level array that will hold three separate integer arrays.
2
jagged[1] = new int[] { 3, 4, 5, 6 };
Assigns an array of four elements to the second index of the jagged array.