ArrayList.ToArray to string array question

W

Walker Moore

I have a method which adds strings to an ArrayList then on the final line
converts it to a string array (which it returns).

ArrayList results = new ArrayList();

... stuff ...

return (string[])results.ToArray(typeof(string));

Is this safe? I don't need to initialize the string[] array do I? I'm
assuming ArrayList.ToArray does that.
 
W

Walker Moore

On Wed, 17 Aug 2011 18:04:25 -0700, Peter Duniho wrote:
[..]
Yes, it does. But you should be using "List<string>" instead of
"ArrayList". Then you can just return "results.ToArray()".

Thank you Peter!
 

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