473,326 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 1775
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

43
by: Mountain Bikn' Guy | last post by:
I have a situation where an app writes data of various types (primitives and objects) into a single dimensional array of objects. (This array eventually becomes a row in a data table, but that's...
24
by: ALI-R | last post by:
Hi All, First of all I think this is gonna be one of those threads :-) since I have bunch of questions which make this very controversial:-0) Ok,Let's see: I was reading an article that When...
4
by: Peter Olcott | last post by:
I want to be able to make my .NET applications run just as fast as unmanaged C++. From my currently somewhat limited understanding of the .NET framework and the C# language, it seems that...
94
by: Peter Olcott | last post by:
How can I create an ArrayList in the older version of .NET that does not require the expensive Boxing and UnBoxing operations? In my case it will be an ArrayList of structures of ordinal types. ...
19
by: ahjiang | last post by:
hi there,, what is the real advantage of boxing and unboxing operations in csharp? tried looking ard the internet but couldnt find any articles on it. appreciate any help
161
by: Peter Olcott | last post by:
According to Troelsen in "C# and the .NET Platform" "Boxing can be formally defined as the process of explicitly converting a value type into a corresponding reference type." I think that my...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.