How to initialize elements in an array 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.

To initialize an array, first you need to declare it.

int[] marks;

Here,

  • int is the datatype
  • [] specifies the size of the array
  • Marks is the name of the array

Now let us initialize the array using the new keyword.

int[] marks = new int[10];

Now let us assign elements to it.

marks[0] = 96;
marks[1] = 90

You can also assign elements like this −

int [] marks = new int[10]  { 78,  67, 88, 56, 90, 77, 76, 70, 60, 70};

Updated on: 30-Jul-2019

156 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements