Best way to “push” into C# array

后端 未结 12 2603
没有蜡笔的小新
没有蜡笔的小新 2021-02-13 14:25

Good day all

So I know that this is a fairly widely discussed issue, but I can\'t seem to find a definitive answer. I know that you can do what I am asking for using a L

12条回答
  •  执笔经年
    2021-02-13 14:29

    There are couple of ways this can be done.

    First, is converting to list and then to array again:

    List tmpList = intArry.ToList();
    tmpList.Add(anyInt);
    intArry = tmpList.ToArray();
    

    Now this is not recommended as you convert to list and back again to array. If you do not want to use a list, you can use the second way which is assigning values directly into the array:

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

    This is the direct approach and if you do not want to tangle with lists and conversions, this is recommended for you.

提交回复
热议问题