470,849 Members | 1,141 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,849 developers. It's quick & easy.

ArrayList.ToArray() Casting for Non System Type

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

}
Mar 3 '06 #1
1 2096
Tedmond wrote:
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();

--

<http://www.midnightbeach.com> Contracting, consulting and training
..NET 2.0 book coming out in June <http://www.midnightbeach.com/.net>
Mar 3 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Mark Oueis | last post: by
4 posts views Thread by Derrick | last post: by
2 posts views Thread by Dirk | last post: by
6 posts views Thread by gane kol | last post: by
2 posts views Thread by Russ Green | last post: by
6 posts views Thread by Iapain | last post: by
1 post views Thread by psrinivasan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.