473,545 Members | 2,443 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1789
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.co m

"Steve" <sh*****@tssg.o rg> wrote in message
news:ca******** *************** ***@posting.goo gle.com...
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.o rg> wrote in
news:ca******** *************** ***@posting.goo gle.com...
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
6848
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 another story.) The data is written once and then read many times. Each primitive read requires unboxing. The data reads are critical to overall app...
24
2583
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 you pass a Value-Type to method call ,Boxing and Unboxing would happen,Consider the following snippet: int a=1355; myMethod(a); ......
4
2958
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 Boxing/Unboxing might present of problem. Since C++ has pointer syntax, I was thinking that this might eliminate the need for Boxing and Unboxing. Am I...
94
5615
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. Thanks.
19
13691
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
7739
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 biggest problem with this process is that the terms "value type" and "reference type" mean something entirely different than what they mean on every...
0
7420
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7680
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7934
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7446
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7778
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6003
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5349
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
1
1033
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
731
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.