Mega Code Archive

 
Categories / C# Book / 03 Collections
 

0356 ArrayList and Linq

If you import the System.Linq namespace, you can convert an ArrayList to a generic List by calling Cast and then ToList: using System; using System.Collections; using System.Collections.Generic; using System.Linq; class Sample { public static void Main() { ArrayList al = new ArrayList(); al.AddRange(new[] { 1, 5, 9 }); List<int> list = al.Cast<int>().ToList(); foreach(int i in list){ Console.WriteLine(i); } } } The output: 1 5 9