471,082 Members | 745 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,082 software developers and data experts.

Casting struct[] to object[]

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

Nov 16 '05 #1
4 15366
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" <no****@nospam.com> wrote in message
news:d1********************@twister2.libero.it...
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


Nov 16 '05 #2
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" <no****@nospam.com> wrote in message
news:d1********************@twister2.libero.it...
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


Nov 16 '05 #3
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" <no****@nospam.com> wrote in
news:d1********************@twister2.libero.it...
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


Nov 16 '05 #4
Fabrizio <no****@nospam.com> wrote:
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.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Jacob Jensen | last post: by
13 posts views Thread by JustSomeGuy | last post: by
3 posts views Thread by Rob Jackson | last post: by
1 post views Thread by rahul8143 | last post: by
44 posts views Thread by Agoston Bejo | last post: by
8 posts views Thread by wkaras | last post: by
3 posts views Thread by LongBow | last post: by
8 posts views Thread by Neha | last post: by
9 posts views Thread by Taras_96 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.