如何将项目从组合框添加到列表集合
How To Add Items From a ComboBox to a List Collection
我有一个带有几个字符串的组合框。我想将这些字符串添加到列表集合中。这是正确的做法吗?
List<string> comboItems = new List<string>();
foreach(string passItems in comboEmail.Items)
{
comboItems.Add(passItems);
}
稍微不同的方式:
List<string> comboItems = comboEmail.Items.Cast<string>().ToList();
这是一个完全有效的方法。
您也可以转换为字符串并使用 AddRange
创建单行。
comboItems.AddRange(cb.comboEmail.Cast<string>());
您的方法很简单。使用它。
有时候简单的foreach
语句里面只有一行代码
将比好看的一行 LINQ
代码更具可读性。
无论如何,两个版本将完成几乎相同的工作。 LINQ
可以比 foreach
更慢
我有一个带有几个字符串的组合框。我想将这些字符串添加到列表集合中。这是正确的做法吗?
List<string> comboItems = new List<string>();
foreach(string passItems in comboEmail.Items)
{
comboItems.Add(passItems);
}
稍微不同的方式:
List<string> comboItems = comboEmail.Items.Cast<string>().ToList();
这是一个完全有效的方法。
您也可以转换为字符串并使用 AddRange
创建单行。
comboItems.AddRange(cb.comboEmail.Cast<string>());
您的方法很简单。使用它。
有时候简单的foreach
语句里面只有一行代码
将比好看的一行 LINQ
代码更具可读性。
无论如何,两个版本将完成几乎相同的工作。 LINQ
可以比 foreach