ArrayList.ToArray() Casting for Non System Type

  • Thread starter Thread starter Guest
  • Start date Start date
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

}
 
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();
 
Back
Top