ArrayList.ToArray to string array question

  • Thread starter Thread starter Walker Moore
  • Start date Start date
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.
 
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!
 
Back
Top