how to add values to an array c# code example

Example 1: c# add element to array

private T[] AddElementToArray<T>(T[] array, T element) {
    T[] newArray = new T[array.Length + 1];
    int i;
    for (i = 0; i < array.Length; i++) {
        newArray[i] = array[i];
    }
    newArray[i] = element;
    return newArray;
}

Example 2: c# append array

List<int> termsList = new List<int>();
for (int runs = 0; runs < 400; runs++)
{
    termsList.Add(value);
}

// You can convert it back to an array if you would like to
int[] terms = termsList.ToArray();

Example 3: append an array in c#

int[] terms = new int[400];
for (int runs = 0; runs < 400; runs++)
{
    terms[runs] = value;
}

Example 4: add item to an array c#

List<string> l = new List<string>(){"one","two","tree","four",};