tags:

views:

3306

answers:

5

Is is possible to create a multidimensional list in C#. I can create an multidimensional array like so:

 string[,] results = new string[20, 2];

but I would like to be able to use some of the features in a list or arraylist like being able to add and delete elements.

+3  A: 

Not exactly. But you can create a list of lists:

var ll = new List<List<int>>();
for(int i = 0; i < 10; ++i) {
    var l = new List<int>();
    ll.Add(l);
}
Mehrdad Afshari
+1  A: 

you just make a list of lists like so:

List<List<string>> results = new List<List<string>>();

and then it's just a matter of using the functionality you want

results.Add(new List<string>()); //adds a new list to your list of lists
results[0].Add("this is a string"); //adds a string to the first list
results[0][0]; //gets the first string in your first list
Joseph
A: 

Depending on your exact requirements, you may do best with a jagged array of sorts with:

List<string>[] results = new { new List<string>(), new List<string>() };

Or you may do well with a list of lists or some other such construct.

John Gietzen
A: 

If you want to modify this I'd go with either of the following:

List<string[]> results;

-- or --

List<List<string>> results;

depending on your needs...

csharptest.net
+5  A: 

You can create a list of lists

   public class MultiDimList: List<List<string>> {  }

or a Dictionary of key-accessible Lists

   public class MultiDimDictList: Dictionary<string, List<int>>  { }
   MultiDimDictList myDicList = new MultiDimDictList ();
   myDicList.Add("ages", new List<int>); 
   myDicList.Add("Salaries", new List<int>); 
   myDicList.Add("AccountIds", new List<int>);
Charles Bretana