Robbe Morris [C# MVP] <in**@turnkeytools.com> wrote:
object[] myobjarray = new object[5];
for(int i=0;i<5;i++)
{
myobjarray[i] = (object)i.ToString();
}
string[] mystrarray = (string[])myobjarray;
for(int i=0;i<5;i++)
{
Console.WriteLine(mystrarray[i]);
}
Console.ReadLine();
Yes, there you've got an object array which happens to contain strings,
rather than a string array. You can't cast an object array to a string
array, despite all the contained objects being strings. (Just think
what would happen if you set myobjarray[i] = new object() afterwards.)
You can use Array.Copy though:
using System;
class Test
{
static void Main()
{
object[] myobjarray = new object[5];
for(int i=0;i<5;i++)
{
myobjarray[i] = (object)i.ToString();
}
string[] mystrarray = new string[myobjarray.Length];
Array.Copy(myobjarray, mystrarray, myobjarray.Length);
foreach (string x in mystrarray)
{
Console.WriteLine (x);
}
}
}
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too