Connecting Tech Pros Worldwide Forums | Help | Site Map

Casting struct[] to object[]

Fabrizio
Guest
 
Posts: n/a
#1: Nov 16 '05
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






Shakir Hussain
Guest
 
Posts: n/a
#2: Nov 16 '05

re: Casting struct[] to object[]


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++;
}

--
Shak
(Houston)


"Fabrizio" <nospam@nospam.com> wrote in message
news:d1DEc.28110$GQ3.718354@twister2.libero.it...[color=blue]
> 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
>
>
>
>
>[/color]


Ken Kolda
Guest
 
Posts: n/a
#3: Nov 16 '05

re: Casting struct[] to object[]


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


"Fabrizio" <nospam@nospam.com> wrote in message
news:d1DEc.28110$GQ3.718354@twister2.libero.it...[color=blue]
> 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
>
>
>
>
>[/color]


Niki Estner
Guest
 
Posts: n/a
#4: Nov 16 '05

re: Casting struct[] to object[]


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

"Fabrizio" <nospam@nospam.com> wrote in
news:d1DEc.28110$GQ3.718354@twister2.libero.it...[color=blue]
> 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
>
>
>
>
>[/color]


Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#5: Nov 16 '05

re: Casting struct[] to object[]


Fabrizio <nospam@nospam.com> wrote:[color=blue]
> I cannot figure why it isn't possible to cast a struct array to an object
> array.[/color]

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.)

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Closed Thread


Similar C# / C Sharp bytes