Answers for "add something to an array c#"

C#
11

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
1

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();
Posted by: Guest on May-05-2020
0

c# add string to array

string[] MyArray = new string[] { "A", "B" };
MyArray = new List<string>(MyArray) { "C" }.ToArray();
//MyArray = ["A", "B", "C"]

raadgames :)
Posted by: Guest on June-11-2021
0

c# add object to array

Array.Resize(ref objArray, objArray.Length + 1);
objArray[objArray.Length - 1] = new Someobject();
Posted by: Guest on May-17-2020
0

append an array in c#

int[] terms = new int[400];
for (int runs = 0; runs < 400; runs++)
{
    terms[runs] = value;
}
Posted by: Guest on March-28-2021
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

Code answers related to "add something to an array c#"

C# Answers by Framework

Browse Popular Code Answers by Language