how to turn ICollection into ArrayList?

G

Guest

since ArrayList implements ICollection, is there a quick way to convert an
ICollection into ArrayList (besides actually iterating through each element
in the ICollection and explicitly adding them to a new ArrayList instance) ??
 
P

Peter Rilling

AddRange simply iterates the collection and copies the items.

MrNobody, I guess the question that I have is, when you refer to "quick",
are you talking about the fewest number of code lines, or the fewest number
of program cycles.

There is no way to automatically turn an ICollection into an ArrayList
without copying the items, unless the collection was originally an
ArrayList. But the simplest way might be to use AddRange like Cor mentioned
below.
 
J

Jay B. Harlow [MVP - Outlook]

Doh!
I should have given a C# sample :-|

ICollection collection;
ArrayList list = new ArrayList(collection);

Jay
 
G

Guest

Yeah I was kinda hoping for something that used least cpu cycles- like a type
cast , butt it's ok because I don't really have THAT many items in this
collection... Thanks everyone
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

A shallow copy is the only way of doing it, as ArrayList has its own
internal way to keep the data. Also with a copy the original collection can
be modified afterwards and the arraylist will not be changed.

Cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top