Initialization And Declaration Of Array In C#

Occasionally, you need to declare multiple variable of same data type. It is complex and time taking process. Just for an example, you have to declare 100 double type of variable then what would you do? Will you declare variable as like:

double num1,num2,num3,num4,num5,num6,num7,num8.... num100;

It is not a good  way to declare multiple variable of same data type. To overcome this situation, array came alive. An array is a collection of same data type. If you have to declare 100 variable of same data type in C#, then you can declare and initialize an array as follow.

double[] num = new double[100];

It stores value in the index position of array starting with 0.

 num[0] = 43;
 num[1] = 433;
 num[2] = 434;
 num[3] = 434;
 num[4] = 34;
 num[5] = 4352;

Now, you can retrieve the value of array by index position. Such as if you have to access 3rd index of value, then you can write as follow:

Leave a Reply

Your email address will not be published. Required fields are marked *