How are values assigned to arrays in C#?


Declaring an array does not initialize the array in the memory. When the array variable is initialized, you can assign values to the array.

Array is a reference type, so you need to use the new keyword to create an instance of the array. For example, assign the values like the following statement −

double[] price = new double[5];
price[0] = 3245.50;
price[1] = 1234.50;
price[2] = 8765.50;
price[3] = 5784.50;
price[4] = 6576.50;

We assigned five values above to the price array. You can assign values to the array at the time of declaration.

double[] price = new double[5] {3245.50, 1234.50, 8765.50, 6576.50;};

Updated on: 21-Jun-2020

52 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements