在.NET中,你如何转换成一个ArrayList到一个强类型的泛型列表,而不使用foreach? [英] In .Net, how do you convert an ArrayList to a strongly typed generic list without using a foreach?
本文介绍了在.NET中,你如何转换成一个ArrayList到一个强类型的泛型列表,而不使用foreach?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请参阅下面的code样品。我需要的的ArrayList
是一个泛型列表。
的ArrayList的ArrayList = GetArrayListOfInts();
清单< INT> intList中=新名单< INT>();//可以这样的foreach凝结成一条线?
的foreach(数组列表中的INT编号)
{
intList.Add(数);
}
返回intList中;
解决方案
请尝试以下
VAR列表= arrayList.Cast< INT>()了ToList()。
这只会但因为它利用在3.5框架中定义特定的扩展方法使用C#编译器3.5工作。
See the code sample below. I need the ArrayList
to be a generic List.
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;
解决方案
Try the following
var list = arrayList.Cast<int>().ToList();
This will only work though using the C# 3.5 compiler because it takes advantage of certain extension methods defined in the 3.5 framework.
这篇关于在.NET中,你如何转换成一个ArrayList到一个强类型的泛型列表,而不使用foreach?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文