0

For my code I need to change a arrayList to a string array.

I did it like this

ArrayList tempList = new ArrayList();
//some code to put strings into the list
stringArray = (string[])tempList.ToArray(typeof(string));

It's the last line I don't get. In the ToArray method you set the type as a string[], so why do you still need the cast?

4
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by antechindia
1

ToArray() attempts to cast all the objects into the type specified (the typeof part). It then returns a generic Array type. If you don't want to use the Array type, you have to cast it into the type you want (the string[] part).

0

string[] strArray = arrList.ToArray(typeof(string)) as string[];

pls try this code for change array list to string liost

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.