Home > DeveloperSection > Articles > Jagged Array In C#.NET

Jagged Array In C#.NET

1 Comment(s)
 19160  View(s)
Rate this:

Jagged Array In C#.NET

Whenever we want to store the information in the hierarchical format or parent child relationship then we use the concept of Jagged Array. Jagged array is defined as array of array. When we used concept of Jagged array then firstly we create parents then we creates child of each parent.


<data type>[ ][ ] <array name>=new int [ <parents>][ ];


<data type>  Types of array such as int, float, string etc.

<array name> represents name of the array.

new is a keyword which is used to allocate memory to array.

[<parennt>][ ]  how many parents we want to create such as 2 ,3 ,5 etc;

Example for declaring parents

·         string  [ ][ ] family=new string[3][ ]; //A Jagged array named family with 3 parents is declared.

Syntax for creating child

<array name>[<parent index>]=new <data type>[<child>];


<array name> represents the name of the array.

<parent index> represents index of the parent which child have been created.

new is a keyword which is used to allocate the memory for child array.

<child> represents number of child needs to be created for parents

Example for creating child array

·         family[0]=new string[4]; //0th index parent had allocated 4 child.

·         family[1]=new string[2]; //1st index parent had allocated 2 child.

Syntax for assigning values in Jagged Array

<array name>[<parent index>][<child index>]=<value>

Where                                                                                                                                                           <array name> represents name of the array in which value needs to be allocated.      

<parent index> represents index of the parent.                                                                    

<child index> represents the index of the array                                                                                                                              

<value> represents the value that needs to be stored in Jagged array.

Example for storing values in Jagged array

·         family[1][1]=45; //45 is stored in Jagged array at 2nd parent of 1st child.

·         family[0][2]=33;//33 is stored in jagged array at 1st parent of 3rd child.

Using loops for inserting/traversing in Jagged array

Example 1:Inserting of jagged array by using loops

void JaggedArray()


            int[][] num = new int[3][];//A Jagged array of name num is created which have 3 parents.

            num[0] = new int[2];//0 index of parent have allocated 2 chileds.

            num[1] = new int[3];//i index of parent have allocated 3 chiled

            num[2] = new int[5];//2 index of parent allocated 5 chiled


            //Storing values in the ragged array

            for (int i = 0; i < num.Length; i++)//outer loop which iterates parent


                for (int j = 0; j < num[i].Length; j++)//inner loop which iterates chiled


                    Console.Write("Enter Values in Ragged Array---> ");//Displaying a message to the console device

                    num[i][j] = Convert.ToInt32(Console.ReadLine());//Storing values in Jagged array




Example 2:Traversing of jagged array by using loops

void traverseArray(int[][] num)


            for (int i = 0; i < num.Length; i++)//outer loop for iterating parents


                for (int j = 0; j < num[i].Length; j++)//Inner loop for iterating chileds


                    Console.WriteLine("Values : {0}", num[i][j]);//displaying values of Jagged Array





By Sunil Singh on   2 months ago

I always seek to read your articles.

Don't want to miss updates? Please click the below button!

Follow MindStick