469,636 Members | 1,516 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,636 developers. It's quick & easy.

Boxing / Unboxing

Hi,

I have a class like:

public ClassA
{
int[] vals1;
int[] vals2;
}
Many instances of ClassA are created and stored in a Hashtable.

Does the following statement cause any boxing/unboxing to be
undertaken (z is an instance of ClassA):

z.vals[0]=z.vals[1]+z.vals[2];
Also:

int i = z.Vals[0]; // This would cause unboxing?

double d = (double)z.Vals[0]; // Is this the same as the previous
statement or would it incur extra penalties casting the value to a
double?

thanks
Nov 16 '05 #1
3 1572
Steve,

No, there is no boxing or unboxing. Basically, the array is of type
int, so no boxing/unboxing needs to take place when setting or getting a
value from it.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Steve" <sh*****@tssg.org> wrote in message
news:ca**************************@posting.google.c om...
Hi,

I have a class like:

public ClassA
{
int[] vals1;
int[] vals2;
}
Many instances of ClassA are created and stored in a Hashtable.

Does the following statement cause any boxing/unboxing to be
undertaken (z is an instance of ClassA):

z.vals[0]=z.vals[1]+z.vals[2];
Also:

int i = z.Vals[0]; // This would cause unboxing?

double d = (double)z.Vals[0]; // Is this the same as the previous
statement or would it incur extra penalties casting the value to a
double?

thanks

Nov 16 '05 #2
"Steve" <sh*****@tssg.org> wrote in
news:ca**************************@posting.google.c om...
Hi,

I have a class like:

public ClassA
{
int[] vals1;
int[] vals2;
}
Many instances of ClassA are created and stored in a Hashtable.

Does the following statement cause any boxing/unboxing to be
undertaken (z is an instance of ClassA):

z.vals[0]=z.vals[1]+z.vals[2];
No, boxing only occurs when you convert a value type (like an integer) to a
reference type (like System.Object).


Also:

int i = z.Vals[0]; // This would cause unboxing?
no; this line takes an integer out of the int[] array and assigns it to a
local variable.
There would be unboxing if Vals was an object[] array, or a collection class
like ArrayList (which contains objects).
double d = (double)z.Vals[0]; // Is this the same as the previous
statement or would it incur extra penalties casting the value to a
double?


Yes and no: again, there is no boxing/unboxing penalty. But of course
converting a 32-bit integer to a 64-bit floating point variable does take
time and (never forget this) may also cost precision!

Niki
Nov 16 '05 #3

int i = z.Vals[0];


So this statement would simple cause the value in the array to be copied
over from the Heap to the stack.

And the statement:

z.Vals[0] = i;

would cause the reverse?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

43 posts views Thread by Mountain Bikn' Guy | last post: by
94 posts views Thread by Peter Olcott | last post: by
19 posts views Thread by ahjiang | last post: by
161 posts views Thread by Peter Olcott | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.