How to initialize an array in C#?

CsharpProgrammingServer Side Programming

All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.

Firstly, declare an array.

int[] rank;

But 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,

int[] rank = new int[5];

You can assign values to an array at the time of declaration.

int[] rank = { 1, 2, 3,4,5};

With that, you can also create and initialize an array in a single line.

int [] rank = new int[5]  { 1,  2, 3, 4, 5};
raja
Published on 18-Sep-2018 12:01:25
Advertisements