Are 2 dimensional Lists possible in c#?

Viewed 330630

I'd like to set up a multidimensional list. For reference, I am working on a playlist analyzer.

I have a file/file-list, which my program saves in a standard list. One line from the file in each list entry.

I then analyze the list with regular-expressions to find specific lines. Some of the data/results from the lines needs to be put into a new multidimensional list; since I don't know how many results/data I'll end up with, I can't use a multidimensional array.

Here is the data I want to insert:

List
(
    [0] => List
        (
            [0] => Track ID
            [1] => Name
            [2] => Artist
            [3] => Album
            [4] => Play Count
            [5] => Skip Count

        )
    [1] => List
        (
And so on....

Real Example:

List
(
    [0] => List
        (
            [0] => 2349
            [1] => The Prime Time of Your Life
            [2] => Daft Punk
            [3] => Human After All
            [4] => 3
            [5] => 2

        )
    [1] => List
        (

So yeah, mlist[0][0] would get TrackID from song 1, mlist[1][0] from song 2 etc.

But I am having huge issues creating a multidimensional list. So far I have come up with

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

But I haven't really had much more progress :(

11 Answers

Well you certainly can use a List<List<string>> where you'd then write:

List<string> track = new List<string>();
track.Add("2349");
track.Add("The Prime Time of Your Life");
// etc
matrix.Add(track);

But why would you do that instead of building your own class to represent a track, with Track ID, Name, Artist, Album, Play Count and Skip Count properties? Then just have a List<Track>.

As Jon Skeet mentioned you can do it with a List<Track> instead. The Track class would look something like this:

public class Track {
    public int TrackID { get; set; }
    public string Name { get; set; }
    public string Artist { get; set; }
    public string Album { get; set; }
    public int PlayCount { get; set; }
    public int SkipCount { get; set; }
}

And to create a track list as a List<Track> you simply do this:

var trackList = new List<Track>();

Adding tracks can be as simple as this:

trackList.add( new Track {
    TrackID = 1234,
    Name = "I'm Gonna Be (500 Miles)",
    Artist = "The Proclaimers",
    Album = "Finest",
    PlayCount = 10,
    SkipCount = 1
});

Accessing tracks can be done with the indexing operator:

Track firstTrack = trackList[0];

Hope this helps.

Just because it hasnt been mentioned yet, sometimes I prefer a List<Dictionary<string, string>>. There are cases where I just dont want to make a custom object for whatever reason, and this super simple data structure is pretty flexible. I realize everything is a string which is innefficient, and it forces you to parse and stringify when incrementing the PlayCount, but it might be worth it to some people.

var trackList = new List<Dictionary<string, string>>();

var track = new Dictionary<string, string>();
track.Add("TrackID"  , "1234");
track.Add("Name"     , "I'm Gonna Be (500 Miles)");
track.Add("Artist"   , "The Proclaimers");
track.Add("Album"    , "Finest");
track.Add("PlayCount", "10");
track.Add("SkipCount", "1");
trackList.Add(track);

This actually has a couple benefits over a custom object, namely, you can add a new key value to some of the tracks, without needing to add it to all the tracks, and the existing code will still work without recompiling. Obviously you have to recompile if you write code to do something with those new keys, but its not required. Second, if you ever store the data in a file you probably want it to be human readable (i would) in which case its all strings anyway. You can add, remove, or modify keys or values with a text editor, and this data structure is able to handle it.

You can create 2D list in C# in this way,

List<List<int>> list = new List<List<int>>{
         new List<int> { 1, 2, 3 },
         new List<int> { 4, 5, 6 },
         new List<int> { 9, 8, 9 }
};
Related