Reputation:
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);
}
Upvotes: 1
Views: 59
Reputation: 32445
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
Upvotes: 0
Reputation: 33823
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>());
Upvotes: 0
Reputation: 3306
A slightly different way:
List<string> comboItems = comboEmail.Items.Cast<string>().ToList();
Upvotes: 1