Easy way to cast an array of strings to an array of objects?

  • Thread starter Thread starter ssg31415926
  • Start date Start date
S

ssg31415926

I need to cast a string[] into an object[] and vice versa.

At the moment, I'm using code like this:

int numOfObjects = Values.Length;
object[] objects = new object[numOfObjects];
for(int i = 0; i < numOfObjects; i++) {
objects = (object)Values;
}

where Values is a string[] passed to the member doing the conversion.

It works but it seems clunky. I feel there should be an easier way.
Is there?
 
use (object[])stringArray

string[] stringItems = "a,b,c,d,e".Split(',');
object[] objectItems = (object[])stringItems;
 
you can't cast string[] to object[], but what you can do is use the
Array.Copy function instead of doing your own loop.
 
Okay, that's what I understood. I just got a bit confused by your
earlier statement. Thanks for clarifying it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top