How are arrays defined in C#?
In C#, arrays can be defined by the following method:
- Initialize an array using an array initializer.
int[] numbers = { 1, 2, 3, 4, 5 };
- fresh
int[] numbers = new int[5];
- Initialize array elements using index method.
int[] numbers = new int[5];
numbers[0] = 1;
numbers[1] = 2;
numbers[2] = 3;
numbers[3] = 4;
numbers[4] = 5;
It is important to note that array indexes start at 0. In the above code, the numbers array has a length of 5 and can store 5 integers.