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

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};
raja
Published on 18-Sep-2018 08:54:49
Advertisements