[c#] Multidimensional Lists in C#

At the moment I am using one list to store one part of my data, and it's working perfectly in this format:

Item
----------------
Joe Bloggs
George Forman
Peter Pan

Now, I would like to add another line to this list, for it to work like so:

NAME                    EMAIL
------------------------------------------------------
Joe Bloggs              [email protected]
George Forman           [email protected]
Peter Pan               [email protected]

I've tried using this code to create a list within a list, and this code is used in another method in a foreach loop:

// Where List is instantiated
List<List<string>> list2d = new List<List<string>>

...

// Where DataGrid instance is given the list
dg.DataSource = list2d;
dg.DataBind();

...


// In another method, where all people add their names and emails, then are added
// to the two-dimensional list
foreach (People p in ppl.results) {
    list.Add(results.name);
    list.Add(results.email);
    list2d.Add(list);
}

When I run this, I get this result:

Capacity Count 
----------------
16       16 
16       16 
16       16
...      ...

Where am I going wrong here. How can I get the output I desire with the code I am using right now?

This question is related to c# list

The answer is


It's old but thought I'd add my two cents... Not sure if it will work but try using a KeyValuePair:

 List<KeyValuePair<?, ?>> LinkList = new List<KeyValuePair<?, ?>>();
 LinkList.Add(new KeyValuePair<?, ?>(Object, Object));

You'll end up with something like this:

 LinkList[0] = <Object, Object>
 LinkList[1] = <Object, Object>
 LinkList[2] = <Object, Object>

and so on...


Highly recommend something more like this:

public class Person {
    public string Name {get; set;}
    public string Email {get; set;}
}

var people = new List<Person>();

Easier to read, easy to code.


If for some reason you don't want to define a Person class and use List<Person> as advised, you can use a tuple, such as (C# 7):

var people = new List<(string Name, string Email)>
{
  ("Joe Bloggs", "[email protected]"),
  ("George Forman", "[email protected]"),
  ("Peter Pan", "[email protected]")
};

var georgeEmail = people[1].Email;

The Name and Email member names are optional, you can omit them and access them using Item1 and Item2 respectively.

There are defined tuples for up to 8 members.

For earlier versions of C#, you can still use a List<Tuple<string, string>> (or preferably ValueTuple using this NuGet package), but you won't benefit from customized member names.


You should use List<Person> or a HashSet<Person>.


Where does the variable results come from?

This block:

foreach (People p in ppl.results) {
    list.Add(results.name);
    list.Add(results.email);
    list2d.Add(list);
}

Should probably read more like:

foreach (People p in ppl.results) {
    var list = new List<string>();
    list.Add(p.name);
    list.Add(p.email);
    list2d.Add(list);
}

Please show more of your code.

If that last piece of code declares and initializes the list variable outside the loop you're basically reusing the same list object, thus adding everything into one list.

Also show where .Capacity and .Count comes into play, how did you get those values?