How to create a one-dimensional dynamic array in C #?

noob question on C #: how to create a one-dimensional dynamic array? And how to change it later?

thank.

+3
source share
5 answers

Instead of using an array, you can use an object List<>in C #.

List<int> integerList = new List<int>();

To iterate over the elements contained in a list, use the operator foreach:

foreach(int i in integerList)
{
    // do stuff with i
}

You can add items to the list object with Add()and functions Remove().

for(int i = 0; i < 10; i++)
{
    integerList.Add(i);
}

integerList.Remove(6);
integerList.Remove(7);

You can convert List<T>to an array using the function ToArray():

int[] integerArray = integerList.ToArray();

Here is the documentation on the site List<>.

+14
source

, , List<T>.

+3

, a List<T> . Array.Resize. :

int[] array = { 1, 2, 3 };
Array.Resize(ref array, 4);
+1

:

ArrayList  //really you should avoid this.
or List<T>

var my_list = new List<Your_List_Type_Here>() (Like List<String>);

:

my_list.Add(Your_Object);

: http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx

, ToArray().

0
0

Source: https://habr.com/ru/post/1741960/


All Articles