System.Array から List への変換 質問する

System.Array から List への変換 質問する

System.Array昨夜、私は次のことは不可能だという夢を見ました。しかし、同じ夢の中でSOの誰かが私に違うことを言いました。そこで、次のものに変換できるかどうか知りたいのです。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);

List<int> lst = ints.OfType<int>(); // not working

ベストアンサー1

痛みから逃れましょう...

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 });

おすすめ記事