find and solve ||
Please wait.....

C# Array:C# Creating an Array of Arrays

C# Array:C# Creating an Array of Arrays

Consider a situaltion where you want to store the names of 20 employees.You may opt to declare 20 variables of the string  datat type and then assign values to those 20 variables.However, this approach may prove to be time-consuming and tedious task because to work with 20 variables,you need to declare and assign values to those vaiables also need to remember the names of the variables to use them in program.A better alternative in this situation is to use an array with a ziee of 20.An array is a coheisve and sigular collection of serverl values of the sam data type.Each value is store in any array element,.The number of elements that an array can store refers to its size.This means that an array of size 20 can have 20 elements only.

 In the memory,an array is represented as a contiguous block of memory space.The amount of memory that an array occupies depends on the size and data type of its elements.For example, if an array has 3 elements of chat data type,then the total amount of memory cooupied by the arraay is 6 byes.

Arrary neet to be declared and initialize before they are used in a program.The syntax to declare an array is as follows:

Data_Type []  Arrary_Name = new Data_Type [Array_Size];

In the preceding syntax,

Data_Type : Refers to the data type of the array elements.
[] : Refers to a pair of square brackets,which indicates that an array is being declared.
Array_Name : Refers to the name of the array.
new : Refers to the new keyword that is used to create an array.
Array_Size : Refers to the six of the  array at is , the number of elements in the array

Let's  now declare an array ,named Days, to store the names of the days in a week as shown in the following code snippet:

string [] Days = new string [7];

In the preceding code snippet,an array named Days of string data type is declared.This implies that the elements of the Days Array of string type. 
In addition,the size of the Days array is 7,which implies that the Days Array can have 7 elements.

You can initialize an array be initializing the array elements. you can do this either while declaring the array of after declaring the array.The syntax to initialize the array at the time of declaration is as follows:

Data_Type [] Arrary_Name  = new Data_Type [] {values1,value2,value3,value4 ....valueN};
Data_Type [] Array_Name = {value1,value2,value3,value4....valueN};

In the preceding syntax,

Data_Type : Refers to the data type of the array elements.
Arrary_Name : Refers to  the name of the array,
new : Refers to the new keyword that is used to create an array.
value1,valued2,value3,value4,...valueN : Refer to the values of that are assigned to the array elements.

You can initialize the Days array created earlier at the time of its declaration as shown in the following code sniped:

 string [] Days = new string []{"Sunday","Monday",Tuesday","Wednesday","Thursday", "Friday", "Saturday"};
string [] Days = []{"Sunday","Monday",Tuesday","Wednesday","Thursday", "Friday", "Saturday"};

In the preceding code snippet,the days array is initialized with seven string type values - Sunday,Monday,Tuesday,Wednesday,Thursday,Friday and Saturday. Note that you do not specify the size of the array if you initialize the array at the time of its declaration. Depending on the values that you provide,the size of the array is implicitly determined.

To initialize the array after declaration you need to access the individual array elemtns and initilize each of them separately.You can acccess an aaray element by using its index,which is an integer that indicates the position of an elements,then the first element has the index 0,the second element has the index 1, and the third elemtns has the index 2.

The syntax to initialize an array element after the array declaration is as follows:

Array_Name [Element_Index]=value;
in the preceding syntax,
Array_Name: Refers to the name of the array.
Elements_Index: Refers to the index of the array element
Value :Refers to the value that is to be assigned to the array element

Full Example

using System;
namespace ArrayApplication
    class DaysArrary
        static void Main(string[] args)
            string [] days = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
            int i;
            /* initialize elements of array n */
            for (i = 0; i < 7; i++)
                Console.WriteLine("Days {0}",days[i]);
Days Sunday
Days Monday
Days Tuesday
Days Wednesday
Days Thursday
Days Friday
Days Saturday

In C#,arrays are broadly classified in the following two types:

1.Single-dimensional arrays
2.Multidimensional arrays

Sundar  Neupane

Sundar Neupane

I like working on projects with a team that cares about creating beautiful and usable interfaces.


Report Response