articles

Home / DeveloperSection / Articles / Array in C#.NET

Array in C#.NET

Anonymous User10499 21-Jan-2011

Sometimes when we make any program then we have to use number of variables for storing data’s. Some data’s are of same data type and some of different data types. When we want to store values of same data type then we create number of variables of same data type which reduce the performance of application as well as efficiency.To improve the efficiency of applicationwe follow the concept of arrays.An array stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type stored at contiguous memory locations. Arrays are defined as collection of similar data types of elements means to say collection of elements which have same data type. By using array we can use concept of loops for inserting values as well as traversing those information.

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

void insertArray()
        {
            int[] num = newint[5]; //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:
void multiDimensionalArray()
        {
            int[,] num = newint[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());
                }
            }
        }
Jagged Array

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
void JaggedArray()
        {
            int[][] num = newint[3][];//A Jagged array of name num is created which have 3 parents.
            num[0] = newint[2];//0 index of parent have allocated 2 chileds.
            num[1] = newint[3];//i index of parent have allocated 3 chiled
            num[2] = newint[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
                }
            }
        }




Updated 29-Nov-2017
I am a content writter !

Leave Comment

Comments

Liked By