System.Array的转换到列表 [英] Conversion of System.Array to List
本文介绍了System.Array的转换到列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
昨天晚上,我有梦想,以下是不可能的。但在同样的梦,从SO有人告诉我,否则。因此,我想知道是否有可能转换的System.Array
到列表
阵列整数= Array.CreateInstance(typeof运算(INT),5);
ints.SetValue(10,0);
ints.SetValue(20,1);
ints.SetValue(10,2);
ints.SetValue(34,3);
ints.SetValue(113,4);
到
列表< INT> LST = ints.OfType<&诠释GT;(); //不工作
解决方案
保存自己有些疼...
INT [] = int型新[] {10,20,10,34,113};清单< INT> LST = ints.OfType< INT>()了ToList()。 //这是不会要快。
也可以直接...
列表< INT> LST =新的List< INT> {10,20,10,34,113};
或...
列表< INT> LST =新的List< INT>();
lst.Add(10);
lst.Add(20);
lst.Add(10);
lst.Add(34);
lst.Add(113);
或...
列表< INT> LST =新的List< INT>(新INT [] {10,20,10,34,113});
或...
VAR LST =新的List< INT>();
lst.AddRange(新INT [] {10,20,10,34,113});
Last night I had dream that the following was impossible. But in the same dream, someone from SO told me otherwise. Hence I would like to know if it it possible to convert System.Array
to List
Array ints = Array.CreateInstance(typeof(int), 5);
ints.SetValue(10, 0);
ints.SetValue(20, 1);
ints.SetValue(10, 2);
ints.SetValue(34, 3);
ints.SetValue(113, 4);
to
List<int> lst = ints.OfType<int>(); // not working
解决方案
Save yourself some pain...
int[] ints = new [] { 10, 20, 10, 34, 113 };
List<int> lst = ints.OfType<int>().ToList(); // this isn't going to be fast.
Can also just...
List<int> lst = new List<int> { 10, 20, 10, 34, 113 };
or...
List<int> lst = new List<int>();
lst.Add(10);
lst.Add(20);
lst.Add(10);
lst.Add(34);
lst.Add(113);
or...
List<int> lst = new List<int>(new int[] { 10, 20, 10, 34, 113 });
or...
var lst = new List<int>();
lst.AddRange(new int[] { 10, 20, 10, 34, 113 });
这篇关于System.Array的转换到列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文