Click here to Skip to main content
15,607,633 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
How to make a dynamic array with C#?
Updated 8-Nov-17 23:53pm
BillWoodruff 20-Jan-12 19:42pm    
I +5'd both answers here, which "covered all the bases:" but, your question should clarify if you are specifically asking about the new "dynamic" functionality added in the most recent .NET, or just using "dynamic" in the general sense of auto-expanding, or contracting, (i.e, not a fixed-size data structure).

If the "dynamic" in this case means anonymous data you can do as:
var items = new[]
    new { user = "some user" }, 
    new { user = "other user" }

If you just want call the variable as dynamic, you can do this:
dynamic items = new ExpandoObject();
items[0] = "Some user";
items[1] = "Some other user";
Share this answer
C# does not have a dynamic array type, or any equivalent of the VB Redim statement.
However, you can use a List<T> as a dynamic array, given that it supports indexed access:
List<string> list = new List<string>();
Share this answer
fjdiewornncalwe 20-Jan-12 12:45pm    
+5. Of course.
Sergey Alexandrovich Kryukov 20-Jan-12 22:35pm    
Sure, a 5.
Joezer BH 10-Feb-13 6:51am    
Same here *****
You can do this making array list

Adding Elements in the Array List:

int a=5;
//this line we have added object of DateTime class
Now we have added three elements of different data type and one object of DateTime class. Add() function from the Array List adds new item to the Array List.

Getting Size of the Array List:

int size=newlist.Count;
By using the property Count of Array List object we can get the size of Array List. As we have added three items because size of the Array List is 3. The size will be stored in a variable named size.

Accessing Items from the Array List:

The main task is to access the items back from Array List. One thing must be kept in mind while accessing the item that we have to cast each item into its proper data type or object.
As we have inserted integer first so we can access it from Array List as follows:

//0 based indexing
int b = (int)newlist[0];
string t=newlist[1] as string;
DateTime dt=(DateTime)newlist[2];

Clearing Array List:

We can clear and delete the all elements of list as follows.


its from[^]
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900