Dynamic array in C#

c#

Question

Is there any method for creating a dynamic array in C#?

1
102
8/11/2011 11:01:27 AM

Accepted Answer

Take a look at Generic Lists.

145
5/8/2015 11:16:10 PM

Expanding on Chris and Migol`s answer with a code sample.

Using an array

Student[] array = new Student[2];
array[0] = new Student("bob");
array[1] = new Student("joe");

Using a generic list. Under the hood the List<T> class uses an array for storage but does so in a fashion that allows it to grow effeciently.

List<Student> list = new List<Student>();
list.Add(new Student("bob"));
list.Add(new Student("joe"));
Student joe = list[1];

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon