Dynamic array in C#


C# Problem Overview

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

C# Solutions

Solution 1 - C#

Take a look at Generic Lists.

Solution 2 - C#

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];

Solution 3 - C#

Sometimes plain arrays are preferred to Generic Lists, since they are more convenient (Better performance for costly computation -Numerical Algebra Applications for example, or for exchanging Data with Statistics software like R or Matlab)

In this case you may use the ToArray() method after initiating your List dynamically

List<string> list = new List<string>();
string[] array = list.ToArray();

Of course, this has sense only if the size of the array is never known nor fixed ex-ante. if you already know the size of your array at some point of the program it is better to initiate it as a fixed length array. (If you retrieve data from a ResultSet for example, you could count its size and initiate an array of that size, dynamically)

Solution 4 - C#

List<T> for strongly typed one, or ArrayList if you have .NET 1.1 or love to cast variables.

Solution 5 - C#

You can do this with dynamic objects:

var dynamicKeyValueArray = new[] { new {Key = "K1", Value = 10}, new {Key = "K2", Value = 5} };

foreach(var keyvalue in dynamicKeyValueArray)

Solution 6 - C#

Use the array list which is actually implement array. It takes initially array of size 4 and when it gets full, a new array is created with its double size and the data of first array get copied into second array, now the new item is inserted into new array. Also the name of second array creates an alias of first so that it can be accessed by the same name as previous and the first array gets disposed

Solution 7 - C#

Dynamic Array Example:

Console.WriteLine("Define Array Size? ");
int number = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Enter numbers:\n");
int[] arr = new int[number];

for (int i = 0; i < number; i++)
	arr[i] = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < arr.Length; i++ )
	Console.WriteLine("Array Index: "+i + " AND Array Item: " + arr[i].ToString());

Solution 8 - C#

you can use arraylist object from collections class

using System.Collections;

static void Main()
  ArrayList arr = new ArrayList();

when you want to add elements you can use


Solution 9 - C#


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSiddiquiView Question on Stackoverflow
Solution 1 - C#Chris Van OpstalView Answer on Stackoverflow
Solution 2 - C#JaredParView Answer on Stackoverflow
Solution 3 - C#Mehdi LAMRANIView Answer on Stackoverflow
Solution 4 - C#MigolView Answer on Stackoverflow
Solution 5 - C#AvnerView Answer on Stackoverflow
Solution 6 - C#Shubham GuptaView Answer on Stackoverflow
Solution 7 - C#sydur.rahman21View Answer on Stackoverflow
Solution 8 - C#adnan AlyaariView Answer on Stackoverflow
Solution 9 - C#Luke T O'BrienView Answer on Stackoverflow