Types of arrays in C#.NET
Each language follows the concept of arrays. Either we are talking about c, c++ or java. Each language has their own syntax as well as type of array. For example c has two types of array single dimension and multi dimension.
Similarly C#.NET has three types of arrays…..
Single Dimension Array
Whenever we want to access the information by using single index then we use the concept of single dimension array. In a single dimension array all the information is stored in a single a row. For inserting/traversing the information of single dimension array we need only one for loop.
Example 1: inserting of array by using for loop
int num = new int; //An array is declared with 5 variables
//Length property of num array is used to calculate the size of the array.
for (int i = 0; i < num.Length; i++)
Console.Write("Enter Any Value--> "); //A message to console device is displayed.
num[i] = Convert.ToInt32(Console.ReadLine()); //A value is stored on i th index of num array.
Multi Dimension Array
Whenever we want to store the values in rows and columns format then we used concept of multidimensional array. Whenever we create an array which has more than one dimension then such type of array is called multidimensional array.
Example: Multidimensional Array:
int[,] num = new int[3, 5];//An array of two dimensional is created which have 3 rows and 5 colomns.
for (int i = 0; i < 3; i++)//outer loops for rows
for (int j = 0; j < 5; j++)//inner loops for columns
Console.WriteLine("Enter Value---> ");
num[i, j] = Convert.ToInt32(Console.ReadLine());
Whenever we want to store the information in hierarchal or parent-child relationship format then we used concept of ragged array. Ragged array is defined as array of array. Suppose we want to store the hierarchy of directory in operating system then we use concept of ragged array.
Example: Jagged Array
int num = new int;//A Jagged array of name num is created which have 3 parents.
num = new int;//0 index of parent have allocated 2 chileds.
num = new int;//i index of parent have allocated 3 chiled
num = new int;//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