Click here to Skip to main content
11,718,589 members (57,876 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
How to make a dynamic array with C#?
Posted 20-Jan-12 5:54am
Edited 10-Feb-13 0:52am
Joezer BH38.2K
v3
Comments
BillWoodruff at 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).
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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";
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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>();
list.Add("Hello");
list.Add("Goodbye");
Console.WriteLine(list[1]);
  Permalink  
Comments
Marcus Kramer at 20-Jan-12 12:45pm
   
+5. Of course.
SAKryukov at 20-Jan-12 22:35pm
   
Sure, a 5.
--SA
Edo Tzumer at 10-Feb-13 6:51am
   
Same here *****
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Its better to use list.It can be used as follows
List li=new List();//creating integer list
li.Add(100);
li.Add(120);//adding items to list

for information on list visit

http://codesmesh.com/dynamic-array-using-list-csharp/[^]
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

You can do this making array list

Adding Elements in the Array List:

int a=5;
strings=“Crunchmodo”;
newlist.Add(a);
newlist.Add(s);
//this line we have added object of DateTime class
newlist.Add(DateTime.Now);
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.

newlist.Clear();

its from
http://crunchmodo.com/c-arraylist/[^]
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 651
1 OriginalGriff 210
2 F-ES Sitecore 185
3 Jochen Arndt 160
4 Maciej Los 130
0 OriginalGriff 535
1 Maciej Los 529
2 Richard MacCutchan 440
3 CHill60 415
4 F-ES Sitecore 415


Advertise | Privacy | Mobile
Web04 | 2.8.150901.1 | Last Updated 10 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100