CSharp - Collection ArrayList class


The nongeneric ArrayList class is used mainly for backward compatibility:

ArrayList al = new ArrayList();
al.Add ("hello");

string first = (string) al [0];
string[] strArr = (string[]) al.ToArray (typeof (string));

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.Generic;
 using System.Linq;
 class MainClass{
    public static void Main(string[] args){
         ArrayList al = new ArrayList();
         al.AddRange (new[] { 1, 5, 9 } );
         List<int> list = al.Cast<int>().ToList();

Cast and ToList are extension methods in the System.Linq.Enumerable class.