Conversion of System.Array to List

Save yourself some pain…

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.

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

Leave a Comment