1321941
1321941

Reputation: 2180

C# Add item to list

I have a list like so:

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

and many objects in the form of:

{'artist' => '....', 'title' => '.....', 'discnumber' => '...'}

Which are being created in a loop. What I am trying to do is add the object to the list.

Thanks

Upvotes: 0

Views: 24927

Answers (2)

Tim Schmelter
Tim Schmelter

Reputation: 460360

I would suggest to create a custom class Song with properties like Artist,Title or Discnumber. Then use a List<Song> instead.

However, if you want to use your strings instead, i assume that you want to keep a csv-format:

foreach( <your Loop> )
{
    songs.Add(String.Join(",", objects));
}

Upvotes: 6

petchirajan
petchirajan

Reputation: 4362

If those are all the object of type string you can add like follows,

List<string> songs = new List<string>(); 
for(int i = 0; i < 10; i++)
{
    songs.Add(i.ToString());
}

Or if you want Key,Value type, you can use dictionary,

Dictionary<string, String> Info = new List<string>(); 
Info.Add("Artist", "Some Artist");
Info.Add("Track", "Some Track");
//You can access the value as follows
string artist = info["Artist"]

Upvotes: 3

Related Questions