C# provides the concept of jagged array which means array of arrays. The elements of jagged array are also arrays. The element size of jagged array can vary.
C# Jagged Arrays Declaration and initialization Example :
1 2 3 4 5 6 7 8 9 10 11 12 |
using System; public class ArrayExample { public static void Main (string[] args) { int[][] arr=new int[2][]; arr[0]=new int[3]{10,12,13}; arr[1]=new int[3]{11,13,14}; } } |
C# jagged Array Example : Traversing the Jagged array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
using System; public class ArrayExample { public static void Main (string[] args) { int[][] arr=new int[2][]; arr[0]=new int[3]{10,12,13}; arr[1]=new int[3]{11,13,14}; for(int i=0;i<arr.Length;i++) { for(int j=0;j<arr[i].Length;j++) { Console.Write(arr[i][j]+ " "); } Console.WriteLine(); } } } |
Output
1 2 3 4 |
10 12 13 11 13 14 |