Convert System.Array to string[]
Solution 1:
How about using LINQ?
string[] foo = someObjectArray.OfType<object>().Select(o => o.ToString()).ToArray();
Solution 2:
Is it just Array
? Or is it (for example) object[]
? If so:
object[] arr = ...
string[] strings = Array.ConvertAll<object, string>(arr, Convert.ToString);
Note than any 1-d array of reference-types should be castable to object[]
(even if it is actually, for example, Foo[]
), but value-types (such as int[]
) can't be. So you could try:
Array a = ...
object[] arr = (object[]) a;
string[] strings = Array.ConvertAll<object, string>(arr, Convert.ToString);
But if it is something like int[]
, you'll have to loop manually.
Solution 3:
You can use Array.ConvertAll
, like this:
string[] strp = Array.ConvertAll<int, string>(arr, Convert.ToString);