See the sample code below. I need ArrayList be a shared list.
ArrayList
ArrayList arrayList = GetArrayListOfInts(); List<int> intList = new List<int>(); //Can this foreach be condensed into one line? foreach (int number in arrayList) { intList.Add(number); } return intList;
Try to execute
var list = arrayList.Cast<int>().ToList();
This will only work using the C # 3.5 compiler, as it uses some extension methods defined in the 3.5 framework.
This is inefficient (it makes an unnecessary intermediate array), but is concise and will work on .NET 2.0:
List<int> newList = new List<int>(arrayList.ToArray(typeof(int)));
How to use the extension method?
From http://www.dotnetperls.com/convert-arraylist-list :
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; } }