Casting struct[] to object[]


F

Fabrizio

Hi
I cannot figure why it isn't possible to cast a struct array to an object
array.

I written a structure like this:

public struct Test {
private int TestA;
private int TestB;

public int A {
get {return this.TestA;}
}
public int B {
get {return this.TestB;}
}
public Test(int a, int b) {
this.TestA=a;
this.TestB=b;
}
}

I have an array of Test struct values (Test[]):
Test[] = new my_struct[2];
my_struct[0] = new Test(1,2);
my_struct[1] = new Test(2,3);

but if I try to cast the structure array to the object:
object[] dest = (object[])my_struct;

the compiler tells me that it isn't possible to cast Test[] to object[],
although it gives me no error if I cast a single Test struct value to
object:
object o = (object)my_struct[0];

Moreover, if i change the struct declaration to a class declaration, no
error occurs for a class array to an object array!

Should I perform some casting operator overload in Test?


TIA
Fabrizio
 
Ad

Advertisements

S

Shakir Hussain

Try this

Test[] my_struct= new Test[2];
my_struct[0] = new Test(1,2);
my_struct[1] = new Test(2,3);

object[] dest = new object[my_struct.Length];
int count = 0;
foreach(Test val in my_struct)
{
dest[count] = new object();
dest[count] = (object)val;
count++;
}
 
K

Ken Kolda

Keep in mind that your line of code:

object o = (object)my_struct[0];

is actually making a copy of the struct and then boxing it within an object
wrapper. So, to cast an array of struct (or any non-reference type such as
int, long, etc.) to an array of object would require the system to allocate
a whole new array and copy/box every element of the source array into it.
Unlike the code which casts an array of reference types to an array of
objects, the two array references in our case would actually point to two
completely different arrays.

Since this is not semantically what you would expect and could be very
costly, the system simply doesn't allow it. If you want to copy it to an
array of objects, you'll have to do it manually.

Ken
 
N

Niki Estner

First of all, conversion from struct to object isn't a cast. It's boxing.
Second, you don't want to cast the array reference you have to, say
System.Array or System.Object, you want to cast every item in the array.
That's more than a cast.
However, the .net language designers thought of that and build a method for
it: Array.Copy.

object[] dest = new object[my_struct.Length];
Array.Copy(my_struct, dest, my_struct.Length);

Copies (and casts) data from one array to another. Fine, isn't it?

Niki
 
Ad

Advertisements

J

Jon Skeet [C# MVP]

Fabrizio said:
I cannot figure why it isn't possible to cast a struct array to an object
array.

Think about how they're both in memory. In an object[], there's a list
of references, one after another, at 4-byte intervals (on x86, anyway).

Your struct takes up 8 bytes - so the array of structs occurs with one
at every 8 bytes.

Now put boxing in as well - your "cast" would actually have to create a
new array, box every element in the original and put a reference to
each boxed value into the new array. Nasty!

The reason it works when you cast (say) MyClass[] to object[] is that
none of that needs to happen - an array of MyClass references *is* an
array of object references already. (The only problem is if you then
try to say foo[0] = new object() - you'll get a runtime exception.)
 

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

Top