Alex Rivera | Logout

Convert System.Array to string[]

Asked 2009-12-28T18:07:43.300
49

I have a System.Array that I need to convert to string[]. Is there a better way to do this than just looping through the array, calling ToString on each element, and saving to a string[]? The problem is I don't necessarily know the type of the elements until runtime.

Edit
Report

1 Answer

74

How about using LINQ?

string[] foo = someObjectArray.OfType<object>().Select(o => o.ToString()).ToArray();
answered 2009-12-28T18:11:03.117

Your Answer