Mike B Mike B - 2 months ago 5
C# Question

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:

Joe Bloggs
George Forman
Peter Pan

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

Joe Bloggs joe@bloggs.com
George Forman george@formangrills.co
Peter Pan me@neverland.com

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;


// 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) {

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?


Why don't you use a List<People> instead of a List<List<string>> ?