Mike B
Mike B

Reputation: 12797

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?

Upvotes: 29

Views: 184899

Answers (7)

Shimmy Weitzhandler
Shimmy Weitzhandler

Reputation: 104692

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.

Upvotes: 9

Nakul Chaudhary
Nakul Chaudhary

Reputation: 26144

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

Upvotes: 1

Rickey
Rickey

Reputation: 7880

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...

Upvotes: 1

Sailing Judo
Sailing Judo

Reputation: 11243

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.

Upvotes: 45

Thomas Levesque
Thomas Levesque

Reputation: 292355

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

Upvotes: 52

Winston Smith
Winston Smith

Reputation: 21884

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);
}

Upvotes: 5

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391276

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?

Upvotes: 0

Related Questions