How can I convert from a System::Collections::ArrayList (containing System::String^ for example) to an array of cli::array<String^>?
How can I convert from a System::Collections::ArrayList (containing System::String^ for example) to an array
Share
If the ArrayList is in your code and you can change it, consider changing it to a
List<String^>. You’ll get type safety, cleaner code when using the class, more available Linq methods, and the normal builtin method on that class will return anarray<String^>without having to jump through any hoops.There’s a builtin method on ArrayList to do this for you. Call ToArray, and specify the type of the array to be returned.
If you’re doing this with other collection types (that aren’t using generics), you’ll have to do something more manual. You could do it by hand:
Or you could use Linq to convert the ArrayList to an
IEnumerable<String^>, and then convert that to an array of strings.