Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multidimensional Lists in C#

Tags:

c#

list

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?

like image 858
Mike B Avatar asked Aug 03 '09 12:08

Mike B


People also ask

What is multi-dimensional list?

They contain a list of elements separated by comma. But sometimes lists can also contain lists within them. These are called nested lists or multidimensional lists.

What is the multidimensional array in C?

A multi-dimensional array can be termed as an array of arrays that stores homogeneous data in tabular form. Data in multidimensional arrays are stored in row-major order. Click here for the Complete Course! The general form of declaring N-dimensional arrays is: data_type array_name[size1][size2]....

What is multidimensional array in C with example?

In C programming, you can create an array of arrays. These arrays are known as multidimensional arrays. For example, float x[3][4];

How are multidimensional arrays stored in C?

The data items in a multidimensional array are stored in the form of rows and columns. Also, the memory allocated for the multidimensional array is contiguous. So the elements in multidimensional arrays can be stored in linear storage using two methods i.e., row-major order or column-major order.


3 Answers

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

like image 143
Thomas Levesque Avatar answered Oct 01 '22 09:10

Thomas Levesque


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.

like image 36
Sailing Judo Avatar answered Oct 01 '22 09:10

Sailing Judo


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.

like image 28
Shimmy Weitzhandler Avatar answered Oct 01 '22 09:10

Shimmy Weitzhandler