Conversion of arrays

M

Mark Broadbent

I have a list of Widget objects that have been added to an ArrayList.

I now would like to create an array of Widget s.
ArrayList.ToArray() returns an array of object type so...
ideally what I would like is something like this

Widget[] myWidgets = (Widget[]) ArrayList.ToArray();

Now, I know why this won't work but is there an easier way to do this
without being forced to iterate through the object array and cast each
element to a new Widget array?

Thanks.

Br,

Mark.
 
M

Morten Wennevik

Hi Mark,

You can use the overloaded ToArray(Type)

Widget[] myWidgets = (Widget[])list.ToArray(typeof(Widget));
 

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