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.
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Generic Generics in Managed C++
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
Simple and basic approach;
Or just an another approach: You can use
personNames.ToArray
too:How about using LINQ?
Is it just
Array
? Or is it (for example)object[]
? If so: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 asint[]
) can't be. So you could try:But if it is something like
int[]
, you'll have to loop manually.You can use
Array.ConvertAll
, like this:This can probably be compressed, but it gets around the limitation of not being able to use Cast<> or Linq Select on a System.Array type of object.