0

I need to store string data in memory in the form of a table, columns are fixed, but rows will be added to the end.

I guess I can use two dimensional string array string[][] if I had known the rows in advance, but that's not the case.

I have currently created a custom class with 3 fields and stored them in a list, but I realise it not very graceful solution, the data will look like this

artist-name1, album-name1, filename1
artist-name2, album-name2, filename2
artist-name3, album-name3, filename3
And so on..

Which data structure is best for this ?

Omar
  • 16,329
  • 10
  • 48
  • 66
Ahmed
  • 14,503
  • 22
  • 92
  • 150

3 Answers3

1

You got lot of choice depending what you want to do with it.

// Usefull if it's not all strings
List<Tuple<int, string, string>> 

or

// A list of array.
List<string[]> 
LightStriker
  • 19,738
  • 3
  • 23
  • 27
  • Is there something like a Tuple in .Net 2.0 ? – Ahmed Nov 04 '12 at 14:52
  • @Ahmed: Damn! Good one, I don't know. But Tuple is only a generic class use to wrap a collection of specific type. You can easily make one on your own, or create a class or a struct to hold the data you want. – LightStriker Nov 04 '12 at 14:57
  • +1 `Tuple<>` was introduced in C# 4.0. But you can create it by yourself, [like here](http://stackoverflow.com/questions/12023164/do-we-have-some-sort-of-a-triple-collection-in-c-sharp/12023228#12023228). – Omar Nov 04 '12 at 15:01
1

You should create a custom class:

public class Song 
{
    public string ArtistName { get; set; }
    public string AlbumName { get; set; }
    public string Filename { get; set; }
}

You should then have a list of these:

var songs = new List<Song>();

This is what classes are for -- don't avoid them.

If you need to "get" each song as a string, you can override .ToString in Song:

public override string ToString()
{
    return string.Format("{0}, {1}, {2}", ArtistName, AlbumName, Filename);
}
Kirk Woll
  • 76,112
  • 22
  • 180
  • 195
  • @LightStriker, I don't think the use-case merits the idiosynchracies of a struct, IMO. – Kirk Woll Nov 04 '12 at 14:57
  • Why not? I don't think he intends to modify the data once the object is created. And considering he is only storing strings... – LightStriker Nov 04 '12 at 15:00
  • @LightStriker, if it's truly immutable, that's fine. But I was not making that assumption. And I do not want the OP to freak out when `songs[0].ArtistName = "Foo"` does not do what he wants. – Kirk Woll Nov 04 '12 at 15:03
0

You could use List<string[]> or List<List<string>>. First case will allow you to add as many rows as you want and the columns will be fixed. The second solution is somewhat more flexible and will let you add as many columns as you want.

List<string[]> list = new List<string[]>();
string[] row = new string[3];
row[0] = "artist-name1";
row[1] = "album-name1";
row[2] = "filename1";
list.Add(row);

Or

List<List<string>> list = new List<List<string>>();
List<string> row = new List<string>();
row.Add("artist-name1");
row.Add("album-name1");
row.Add("filename1");
list.Add(row);
Nikola Davidovic
  • 8,556
  • 1
  • 27
  • 33
  • Thanks Nick for the elaborative answer :) – Ahmed Nov 04 '12 at 15:02
  • @Ahmed You're welcome, this is a straight answer to your question but I totally agree with others that your first attempt was the correct one. As I commented your question, you should use classes to encapsulate your complex data types (even is cases like this one). – Nikola Davidovic Nov 04 '12 at 15:06