What is the method of defining and assigning values to an array in c#?

There are two ways to define and initialize arrays in C#.

  1. Using array initializer:
int[] array = {1, 2, 3, 4, 5};
  1. Utilize the new keyword:
int[] array = new int[5];
array[0] = 1;
array[1] = 2;
array[2] = 3;
array[3] = 4;
array[4] = 5;

In the first method, using curly brackets to directly initialize an array and assign values is simple and convenient. In the second method, creating an array object with the new keyword first, and then assigning values one by one using indexes.

Leave a Reply 0

Your email address will not be published. Required fields are marked *