ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

Upgrade

Upgrades an ArrayList to a generic List

Source

/// <summary>
/// Converts an arraylist to a generic List
/// </summary>
/// <typeparam name="T">The type of the elements in the arraylist</typeparam>
/// <param name="l">The arraylist</param>
/// <returns></returns>
public static List<T> Upgrade<T>(this ArrayList l)
{
    var list = new List<T>();

    foreach (T entry in l)
    {
        list.Add(entry);
    }

    return list;
}

Example

ArrayList l = new ArrayList();
 l.Add("a");
 l.Add("b");

 var list = l.Upgrade<string>();

Author: C.F.Meijers

Submitted on: 11 dec. 2007

Language: C#

Type: System.Collections.ArrayList

Views: 5118