昨晚我做了一个梦,下面的事情是不可能的。但在同一个梦里,有人告诉我事实并非如此。因此,我想知道是否有可能转换系统。数组到列表
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
你可以试试你的代码:
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);
int[] anyVariable=(int[])ints;
然后你可以使用anyVariable作为你的代码。
给自己省点痛苦吧……
using System.Linq;
int[] ints = new [] { 10, 20, 10, 34, 113 };
List<int> lst = ints.OfType<int>().ToList(); // this isn't going to be fast.
也可以…
List<int> lst = new List<int> { 10, 20, 10, 34, 113 };
还是……
List<int> lst = new List<int>();
lst.Add(10);
lst.Add(20);
lst.Add(10);
lst.Add(34);
lst.Add(113);
还是……
List<int> lst = new List<int>(new int[] { 10, 20, 10, 34, 113 });
还是……
var lst = new List<int>();
lst.AddRange(new int[] { 10, 20, 10, 34, 113 });
你可以试试你的代码:
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);
int[] anyVariable=(int[])ints;
然后你可以使用anyVariable作为你的代码。