Jam*_*ruk 56 .net c# list arraylist generic-list
请参阅下面的代码示例.我需要ArrayList一个通用列表.我不想用foreach.
ArrayList arrayList = GetArrayListOfInts();
List<int> intList = new List<int>();
//Can this foreach be condensed into one line?
foreach (int number in arrayList)
{
intList.Add(number);
}
return intList;
Run Code Online (Sandbox Code Playgroud)
Jar*_*Par 112
请尝试以下方法
var list = arrayList.Cast<int>().ToList();
Run Code Online (Sandbox Code Playgroud)
这只能使用C#3.5编译器,因为它利用了3.5框架中定义的某些扩展方法.
mqp*_*mqp 10
这是低效的(它不必要地创建一个中间数组)但是简洁并且可以在.NET 2.0上运行:
List<int> newList = new List<int>(arrayList.ToArray(typeof(int)));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
49351 次 |
| 最近记录: |