Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert List<string> to ArrayList

Who knows the easiest way to convert a List of strings to an ArrayList?

I tried setting (ArrayList) before the code but this doesn't do anything.

like image 252
Ozkan Avatar asked Jul 07 '11 12:07

Ozkan


2 Answers

Sure:

ArrayList arrayList = new ArrayList(list);

That works because List<T> implements ICollection.

However, I'd strongly advise you to avoid ArrayList (and other non-generic collections) if at all possible. Can you refactor whatever code wants an ArrayList to use a generic collection instead?

like image 57
Jon Skeet Avatar answered Oct 15 '22 20:10

Jon Skeet


ArrayList has a constructor which accepts an ICollection. Since List<T> implements ICollection, the following should work:

var myArrayList = new ArrayList(myList);
like image 37
Heinzi Avatar answered Oct 15 '22 20:10

Heinzi