Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically Create an Array in C# [duplicate]

Tags:

c#

How can I dynamically create an array in C#?

like image 586
saurabh Avatar asked May 19 '09 05:05

saurabh


People also ask

Can you dynamically create an array in C?

We can create an array of pointers also dynamically using a double pointer. Once we have an array pointers allocated dynamically, we can dynamically allocate memory and for every row like method 2.

How do you create a dynamic array?

Dynamic arrays in C++ are declared using the new keyword. We use square brackets to specify the number of items to be stored in the dynamic array. Once done with the array, we can free up the memory using the delete operator. Use the delete operator with [] to free the memory of all array elements.

What is dynamic array How is it created in C?

Dynamic arrays are very useful data structures. They can be initialized with variable size at runtime. This size can be modified later in the program to expand (or) shrink the array. Unlike fixed-size arrays and Variable Length Arrays, Dynamically sized arrays are allocated in a heap.

Can we create a dynamic array?

A simple dynamic array can be constructed by allocating an array of fixed-size, typically larger than the number of elements immediately required.


1 Answers

I'd like to add to Natrium's answer that generic collections also support this .ToArray() method.

List<string> stringList = new List<string>();
stringList.Add("1");
stringList.Add("2");
stringList.Add("3");
string[] stringArray = stringList.ToArray();
like image 171
Gerrie Schenck Avatar answered Oct 11 '22 01:10

Gerrie Schenck