开发者

Convert List<string> to ArrayList

开发者 https://www.devze.com 2023-03-18 04:20 出处:网络
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.Sure:

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.


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?


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

var myArrayList = new ArrayList(myList);
0

精彩评论

暂无评论...
验证码 换一张
取 消