Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

object to string array

Tags:

c#

c#-3.0

c#-4.0

I am trying to convert an object (is declared here as 'obj': object is array, primitive) to a string array.

object can be anything uint[], int16[], etc.

I have been trying to use

string[] str = Array.ConvertAll<object, string>((object[])obj, Convert.ToString); 

The problem occurs when I try to cast the unknown type object into object[]. I have been getting casting error.

One attempt I made, which failed, was using

object[] arr = (object[])obj; 

or

IEnumerable<object> list = obj as IEnumerable<object> object[] arr = (object[])list; 

I saw postings regarding value type and reference type issue on casting.

Would there be a simple code that can handle casting to object[] regardless of type of object, as long as it is an array ? I am trying to avoid manual handling of every possible type casting.

thanks in advance

like image 911
swcraft Avatar asked May 24 '12 21:05

swcraft


People also ask

How do I convert an array of objects to a string array?

As list. toArray() returns an Object[], it can be converted to String array by passing the String[] as parameter.

How do you turn an object into an array?

To convert an object to an array you use one of three methods: Object. keys() , Object. values() , and Object. entries() .

How do you cast an object to a string array in C#?

object can be anything uint[], int16[], etc. string[] str = Array. ConvertAll<object, string>((object[])obj, Convert. ToString);

Can you convert object to string?

We can convert Object to String in java using toString() method of Object class or String. valueOf(object) method. You can convert any object to String in java whether it is user-defined class, StringBuilder, StringBuffer or anything else.


2 Answers

You can use the fact that every array implements IEnumerable:

string[] arr = ((IEnumerable)obj).Cast<object>()                                  .Select(x => x.ToString())                                  .ToArray(); 

This will box primitives appropriately, before converting them to strings.

The reason the cast fails is that although arrays of reference types are covariant, arrays of value types are not:

object[] x = new string[10]; // Fine object[] y = new int[10]; // Fails 

Casting to just IEnumerable will work though. Heck, you could cast to Array if you wanted.

like image 74
Jon Skeet Avatar answered Sep 20 '22 05:09

Jon Skeet


If it's always a collection of some type (array, list, etc ...) then try casting back to plain old System.Collections.IEnumerable and go from there

string[] str = ((System.Collections.IEnumerable)obj)   .Cast<object>()   .Select(x => x.ToString())   .ToArray(); 

Here is a more thorough implementation that handles non-collections as well

static string[] ToStringArray(object arg) {   var collection = arg as System.Collections.IEnumerable;   if (collection != null) {     return collection       .Cast<object>()       .Select(x => x.ToString())       .ToArray();   }    if (arg == null) {     return new string[] { };   }    return new string[] { arg.ToString() }; } 
like image 35
JaredPar Avatar answered Sep 20 '22 05:09

JaredPar