【发布时间】:2010-12-06 15:14:38
【问题描述】:
我正在使用 C# 并以 .NET Framework 3.5 为目标。我正在寻找一小段简洁高效的代码来将ListBox 中的所有项目复制到List<String>(通用List)。
目前我有类似下面的代码:
List<String> myOtherList = new List<String>();
// Populate our colCriteria with the selected columns.
foreach (String strCol in lbMyListBox.Items)
{
myOtherList.Add(strCol);
}
这当然可行,但我不禁觉得必须有更好的方法来使用一些较新的语言功能来做到这一点。我正在考虑类似 List.ConvertAll 方法的东西,但这仅适用于通用列表而不是 ListBox.ObjectCollection 集合。
【问题讨论】:
-
列表框中的对象是什么?
-
对不起 - 应该明确声明它们是字符串。
-
我喜欢的扩展方法
-
不是最简洁,但有多种选择here
标签: c# generics collections type-conversion