Answers for "how to append an element to an array in c#"

C#
9

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;
}
Posted by: Guest on November-24-2020
8

c# add to array

var Freds = new [] { "Fred", "Freddy" };
Freds = Freds.Concat(new [] { "Frederick" }).ToArray();
Posted by: Guest on May-01-2020
0

how to append an element to an array in c#

//1. declare the array with a specified length:
int[] MyArray= new int[10];
for (int i=0; i<11; i++){
  //here i insert the elements
  MyArray[i]= i*2;
}
/*attention! in this way you are only putting in values in the array, you
are not modifying the length of the array, if you try to add more elements
than the length of the array it will throw a range out of index exeption*/

//2. you can also use a variable to define how long the array is:
//in this way you can have a different array length avery time you run the program
string Input= Console.ReadLine();
int[] MyArray= new int[Input.Length()];
for (int i=0; i<input.Length(); i++){
  //here i insert the elements
  MyArray[i]= i+1;
}

/*concluding the syntax is:
name of the array[index where you want to assign the value]= value you want to assign;
*/
Posted by: Guest on March-25-2021
-2

add item to an array c#

List<string> l = new List<string>(){"one","two","tree","four",};
Posted by: Guest on October-01-2020

Code answers related to "how to append an element to an array in c#"

C# Answers by Framework

Browse Popular Code Answers by Language