Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create a one-dimensional dynamic array in c#?

Tags:

arrays

c#

dynamic

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

thanks.

like image 293
Saska Avatar asked Dec 03 '22 05:12

Saska


1 Answers

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

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

To iterate on items contained in the list, use the foreach operator :

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

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

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

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

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

int[] integerArray = integerList.ToArray();

Here is the documentation on List<> object.

like image 151
Thibault Falise Avatar answered Dec 11 '22 17:12

Thibault Falise