Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Array Initialization in C#



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};
Updated on: 2020-06-20T11:00:30+05:30

103 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements