简体   繁体   中英

How To Add Items From a ComboBox to a List Collection

I've got a combo box with a few strings. I would like to add those string to a List collection. Is this the correct way of doing it?

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

foreach(string passItems in comboEmail.Items)
{
    comboItems.Add(passItems);
}

略有不同的方式:

List<string> comboItems = comboEmail.Items.Cast<string>().ToList();

That is a perfectly valid approach.

You can also cast to string and use AddRange to create a one-liner.

comboItems.AddRange(cb.comboEmail.Cast<string>());

Your approach is simple enough. Use it.

Sometimes simple foreach statement with one line code inside
will be more readable then nice looking one line LINQ code.

Anyway both versions will be doing almost same work. LINQ can be even slowly then foreach

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