How to add data to an array in c#?

There are several ways to create an array and add data in C#.

  1. Using array initializer: you can directly add data when defining an array. For example:
int[] numbers = {1, 2, 3, 4, 5};
  1. Assign values using index: You can assign values one by one by using the index of the array. An example is shown below:
int[] numbers = new int[5];
numbers[0] = 1;
numbers[1] = 2;
numbers[2] = 3;
numbers[3] = 4;
numbers[4] = 5;
  1. You can use a loop structure to assign values one by one. An example is shown below:
int[] numbers = new int[5];
for (int i = 0; i < numbers.Length; i++)
{
    numbers[i] = i + 1;
}

Data can be added to an array using any method.

bannerAds