ArrayList.ToArray() Casting for Non System Type

G

Guest

Dear all,

How can I convert a list of items from ArrayList into an array of non system
type objects?

e.g. The following codes have error:

public class myClass
{
int i;
}

void Main()
{
ArrayList list = new ArrayList(0);

list.add(new myClass());
list.add(new myClass());
...

myClass[] cls = list.ToArray(); // this line doesn't work

}
 
J

Jon Shemitz

Tedmond said:
myClass[] cls = list.ToArray(); // this line doesn't work

myClass[] cls = (myClass[]) list.ToArray(typeof(myClass));

Of course, if you use a (2.0) List<MyClass> instead of an ArrayList,
you can just write

myClass[] cls = list.ToArray();
 

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