Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

How can I convert/dump an arraylist into a list? I'm using arraylist because I'm using the ASP.NET profiles feature and it looked like a pain to store List in profiles.

Note: The other option would be to wrap the List into an own class and do away with ArrayList.

http://www.ipreferjim.com/site/2009/04/storing-generics-in-asp-net-profile-object/

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
538 views
Welcome To Ask or Share your Answers For Others

1 Answer

The easiest way to convert an ArrayList full of objects of type T would be this (assuming you're using .NET 3.5 or greater):

List<T> list = arrayList.Cast<T>().ToList();

If you're using 3.0 or earlier, you'll have to loop yourself:

List<T> list = new List<T>(arrayList.Count);

foreach(T item in arrayList) list.Add(item);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...