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#.
- Using array initializer:
int[] array = {1, 2, 3, 4, 5};
- 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.