Convert System.Array to string[]

KrisTrip picture KrisTrip · Dec 28, 2009 · Viewed 60.8k times · Source

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.

Answer

Craig Stuntz picture Craig Stuntz · Dec 28, 2009

How about using LINQ?

string[] foo = someObjectArray.OfType<object>().Select(o => o.ToString()).ToArray();