Casting object array to MyObject array

Let’s say that I have an ArrayList of MyObject instances.

ArrayList.ToArray() returns an array of object ( object[] ). Now I want to cast the object[] to MyObject[] without creating a new array.

This throws a InvalidCastException:


And this always returns null:

arrayList.ToArray() as MyObject[]

Any other ideas?


Oops, I didn’t see that the ToArray method has an overload that takes a Type. :-[

This seems to do what I want:


I can’t wait for Generics to come :slight_smile: