简体   繁体   English

C#将项目添加到列表

[英]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 . 我建议创建一个具有ArtistTitleDiscnumber类的属性的自定义类Song Then use a List<Song> instead. 然后使用List<Song>代替。

However, if you want to use your strings instead, i assume that you want to keep a csv-format: 但是,如果您想使用字符串代替,我假设您想保留csv格式:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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