
You want to convert an ArrayList instance in your C# program to a List instance. This can be done in a variety of ways. One way uses the extension method syntax in the C# language to formulate an effective ToList method.
To start, this program introduces the static Extensions class and the ToList generic method inside that class. ToList here is a generic extension method; it has a type parameter T. The 'this' modifier is used on the ArrayList argument to specify that the method is an extension. Inside ToList, we allocate a List with the correct capacity, and then simply copy each element.
This C# program converts an ArrayList to a List. It introduces a generic extension method.
Program that converts ArrayList to List [C#]
using System;
using System.Collections;
using System.Collections.Generic;
static class Extensions
{
/// <summary>
/// Convert ArrayList to List.
/// </summary>
public static List<T> ToList<T>(this ArrayList arrayList)
{
List<T> list = new List<T>(arrayList.Count);
foreach (T instance in arrayList)
{
list.Add(instance);
}
return list;
}
}
class Program
{
static void Main()
{
// Create ArrayList.
ArrayList arrayList = new ArrayList();
arrayList.Add(1);
arrayList.Add(2);
arrayList.Add(3);
// Use extension method.
List<int> list = arrayList.ToList<int>();
foreach (int value in list)
{
Console.WriteLine(value);
}
}
}
Output
1
2
3
Testing the method. The Main entry point tests the ToList method on an ArrayList instance. You can see that the method works correctly for the List of ints. I also tested the method using string literals in the ArrayList and it worked correctly. Just change the List<int> to a List<string> and also change int to string in the other places.

In this example, we looked at one way you can convert an ArrayList to a List instance. Another approach you can use is to convert the ArrayList to an Array, and then use the new List constructor with that Array instance as the parameter. The approach here is not necessarily better.
Convert ArrayList to Array Cast Examples