简体   繁体   中英

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

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

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"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM