Conversion of arrays

  • Thread starter Thread starter Mark Broadbent
  • Start date Start date
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.
 
Hi Mark,

You can use the overloaded ToArray(Type)

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