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.
+10
A:
How about using LINQ?
string[] foo = someObjectArray.OfType<object>().Select(o => o.ToString()).ToArray();
Craig Stuntz
2009-12-28 18:11:03
I don't seem to have access to the .Select method from my Array. Am I missing something?
KrisTrip
2009-12-28 18:18:50
Yes, add `System.Linq` to your `using`
Craig Stuntz
2009-12-28 18:19:56
Make sure you have a using for System.Linq in the class.
TheHurt
2009-12-28 18:20:14
Make sure you are using C# 3, and that you've included the System.Linq namespace.
LBushkin
2009-12-28 18:20:16
I have System.Linq in my using statements. Here is the error message I get: Error 1 'System.Array' does not contain a definition for 'Select' and no extension method 'Select' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)
KrisTrip
2009-12-28 18:23:28
@Craig, TheHurt, and LBushkin: `System.Array` doesn't implement `IEnumerable<T>`, so most LINQ extensions won't work. There is, however, an `OfType<T>` extension method on `IEnumerable` (which `System.Array` *does* implement), so it would have to be `string[] foo = someArray.OfType<object>().Select(o=>o.ToString()).ToArray();`.
P Daddy
2009-12-28 18:35:22
You also need `System.Collections.Generic` in your `using` for `IEnumerable<T>`.
Craig Stuntz
2009-12-28 18:38:05
Updated thanks to P Daddy; I forgot that `System.Array` is unusual.
Craig Stuntz
2009-12-28 18:40:43
+3
A:
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.
Marc Gravell
2009-12-28 18:30:13
It is just Array and it could actually contain value types (probably will).
KrisTrip
2009-12-28 18:37:05
@KrisTrip - the *variable* might be "just Array", but the object won't be; it has a definite array type. There is also a **big** difference between "contain value types" (which `object[]` can do), and "is a value-type array" (such as `int[]`). This difference matters in this case. Can you clarify what **exactly** the `Array` instance is?
Marc Gravell
2009-12-28 19:04:14
I get the Array by using a ToArray method. I don't know until runtime but it could be an int[], double[], float[], string[], or Complex[] (user defined object)
KrisTrip
2009-12-28 19:28:18
In that case, either you'll have to loop manually (like you are) or do some pretty messy reflection (`MakeGenericMethod`). The former would be preferable.
Marc Gravell
2009-12-28 19:45:12