Before reading this article, I highly recommend reading the previous part:
Question: What is a jagged array in C#?
Answer
A jagged array is an array of arrays.
Jagged array
- The elements of a jagged array can be of varying dimensions and sizes.
- In a jagged array the number of columns are determined depending on the requirements for each row.
A jagged array looks like this,

We have the three employees Sourabh, Shaili and Shalin. Sourabh has three qualifications, Shaili has one qualification and Shalin has two qualifications. Here each employee has a different number of qualifications.

Now, I want a data structure that stores this varying number of qualifications as a jagged array. Let's see an example of one of the choices for a jagged array.
Step 1
Open Visual Studio then create a new project and name it InterviewQuestionPart2.
Step 2


Step 2
Now the qualifications are for a string so I will create a string jagged array and initialize the three Arrays of string Array. For example,
- The first employee Sourabh has 3 qualifications so the array of strings array has 3 elements.
- The second employee Shaili has 1 qualification so the array of strings array has 1 element.
- The third employee Shalin has 2 qualifications so the array of strings array has 2 elements.
- Create an array of strings array depending on requirements.
- Now we store the values of the first string array elements, like the first employee Sourabh has the three qualifications "Bachelors", "Masters" and "Doctorate" and we store the values.
- Now initialize the second string array within the jagged array and store the values into the elements of the second string array like second employee Shaili has 1 qualification "Bachelors" so we store that value.
- Now initialize the third string array within the jagged array and store the values to the elements of the third string array like the third employee Shalin has the 2 qualifications "Bachelors" and "Masters" so we store those values.
Create a jagged Array and String Array
Look at the following code,
- using System;
- namespace InterviewQuestionPart2
- {
- class Program
- {
- static void Main(string[] args)
- {
- //Creations of jaggedArray
- string[][] jaggedArray= new string[3][];
- //Array of string Array
- jaggedArray[0] = new string[3];
- jaggedArray[1] = new string[1];
- jaggedArray[2] = new string[2];
- //values of 1st string Array
- jaggedArray[0][0] = "Bachelores";
- jaggedArray[0][1] = "Masters";
- jaggedArray[0][2] = "Doctorate";
- //values of 2nd string Array
- jaggedArray[1][0]= "Bachelores";
- //values of 3rd string Array
- jaggedArray[2][0] = "Bachelores";
- jaggedArray[2][1] = "Masters";
- }
- }
- }




Sr KarthigaPosted Mar 8, 2016, 11:28 AM
good one
Sr KarthigaPosted Mar 8, 2016, 11:28 AM
nice explanation