473,721 Members | 2,182 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

advantage of boxing and unboxing

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

Nov 28 '06 #1
19 13718
<ah*****@gmail. comwrote in message
news:11******** *************@1 4g2000cws.googl egroups.com...
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.
The advantage of boxing is that you can pass, say, an integer around as an
object. The advantage of unboxing is you get you native integer performance
back.

Michael
Nov 28 '06 #2
ah*****@gmail.c om wrote:
what is the real advantage of boxing and unboxing operations in csharp?
tried looking ard the internet but couldnt find any articles on it.
You can have methods that take or return ANY type of value. How would
you implement Reflection's GetField or SetProperty methods without
boxing?

Secondarily, you can have data structures that can hold ANY type of
value, like an object[] or 1.x's ArrayList and HashTable.

--

..NET 2.0 for Delphi Programmers
www.midnightbeach.com/.net
What you need to know.
Nov 28 '06 #3
It takes a lot of resources, so it should be minimized.

Robin S.
---------------
<ah*****@gmail. comwrote in message
news:11******** *************@1 4g2000cws.googl egroups.com...
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

Nov 28 '06 #4
what resources does it take? and when you say 'it' takes a lot of
resources, would something as simple as the following take alot of
resources?

class Test
{
static void Main() {
int i = 123;
object o = i; //boxing
int j = (int) o; //unboxing
}
}

I ask as a complete beginner, thankyou for your time!

Gary.

RobinS wrote:
It takes a lot of resources, so it should be minimized.

Robin S.
---------------
<ah*****@gmail. comwrote in message
news:11******** *************@1 4g2000cws.googl egroups.com...
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
Nov 28 '06 #5
RobinS wrote:
>
It takes a lot of resources, so it should be minimized.
Not really. It does consume resources and is not free, so should be
minimized, but it certainly doesn't consume a "lot" of resources.

Boxing creates a reference type, on the heap, that contains the boxed
value and a type code. So, there's the modest cost of allocation
(which does push the system closer to the next garbage collection) and
the modest cost of copying data.

Unboxing copies the value type from the boxed value to a new copy of
the value type. So, there's the modest cost of copying the value type.

I did some tests a while back. Populating an ArrayList with integers,
then reading them all back, takes about twice as long as populating a
List<intwith integers and then reading them all back. A large ratio,
and one that can make a difference in a bottleneck routine, but not a
large absolute difference - neither operation is particularly slow.

--

..NET 2.0 for Delphi Programmers
www.midnightbeach.com/.net
What you need to know.
Nov 28 '06 #6
"RobinS" <Ro****@NoSpam. yah.nonewrote in message
news:M9******** *************** *******@comcast .com...
It takes a lot of resources, so it should be minimized.
Unless your app is doing pure processing it will make very little
difference.

Michael
Nov 28 '06 #7
No, something like the code you posted would not "take a lot of
resources".

IMHO, RobinS's post was too brief to the topic justice.

The only cases in which boxing / unboxing is likely to cause problems
are when:

1. You're doing it in bottleneck code. That is, code that is run deep
within nested loops and is CPU bound. A performance profiler will tell
you which methods in your program are taking up all the time. If you
look at one of the heavy-hitters and it's using Object and boxing and
unboxing then consider whether you can't change the design to do things
another way (like using generics) to avoid the extra time taken boxing
and unboxing.

2. You're doing it in a shared library and you're not sure whether the
code will be heavily used later on.

In my experience, boxing and unboxing becomes a problem only when it's
"deep down" into your software layers and you discover that your
program is creating millions of little boxed objects on the heap,
objects which the garbage collector later has to go and collect. In
other words, a harmless little routine that boxes a few hundred values
is just that: harmless. At least, it's harmless until you call it in
sixteen places within a higher-level class, which is then invoked in
seven places in a yet-higher-level class, which is then... and suddenly
you've got a half million boxed objects cluttering up memory.

The simple box and unbox that you posted, done once at the Main()
method level, is by comparison completely harmless.

In answer to your specific question, boxing / unboxing adds the
following costs:
1. The cost to box the value: allocate space for it on the heap and
copy the value to the allocated box.
2. The cost to unbox the value: one extra memory dereference in order
to find the exactly location of the value to copy off the heap.
3. Garbage collector costs to reclaim the box when it's no longer
needed.
In small quantities these costs are negligable. The only catch is that
it's very easy to do massive numbers of box / unbox operations in a few
short lines of code.

ga********@mywa y.com wrote:
what resources does it take? and when you say 'it' takes a lot of
resources, would something as simple as the following take alot of
resources?

class Test
{
static void Main() {
int i = 123;
object o = i; //boxing
int j = (int) o; //unboxing
}
}

I ask as a complete beginner, thankyou for your time!

Gary.

RobinS wrote:
It takes a lot of resources, so it should be minimized.

Robin S.
---------------
<ah*****@gmail. comwrote in message
news:11******** *************@1 4g2000cws.googl egroups.com...
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
>
Nov 29 '06 #8
In response to all the postings in response to this posting:
Sorry. I read a few books by Francesco Balena, and he
says that this should be minimized where possible. I went
back to find a specific reference, and this is one thing he
says that is confirmed by the other responses to this post:

In general, it doesn't make sense to compare the performance
of a method that uses boxing with a similar method that doesn't.
An informal benchmark shows that a tight loop that calls a function
that requires boxing can be up to 30 times slower than a loop that
doesn't use boxing. However, you must repeat the loop 10 million
times to see a meaningful difference in absolute terms, so in
practice you should worry about boxing only in time-critical
code sections.

So I apologize, and will be more careful in the future with
information that I post.

Robin S.
--------------------------------------
"RobinS" <Ro****@NoSpam. yah.nonewrote in message
news:M9******** *************** *******@comcast .com...
It takes a lot of resources, so it should be minimized.

Robin S.
---------------
<ah*****@gmail. comwrote in message
news:11******** *************@1 4g2000cws.googl egroups.com...
>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


Nov 30 '06 #9

RobinS wrote:
In response to all the postings in response to this posting:
Sorry. I read a few books by Francesco Balena, and he
says that this should be minimized where possible. I went
back to find a specific reference, and this is one thing he
says that is confirmed by the other responses to this post:

In general, it doesn't make sense to compare the performance
of a method that uses boxing with a similar method that doesn't.
An informal benchmark shows that a tight loop that calls a function
that requires boxing can be up to 30 times slower than a loop that
doesn't use boxing. However, you must repeat the loop 10 million
times to see a meaningful difference in absolute terms, so in
practice you should worry about boxing only in time-critical
code sections.

So I apologize, and will be more careful in the future with
information that I post.
Robin,

One of the things I like about this newsgroup is that while we do
encourage you to double-check before you post, nobody here is going to
flame you if you make a mistake. Instead, we'll just correct you, as we
did.

Some of the older newsgroups are filled with screamers who think that
it's their life mission to blast anyone who doesn't know as much as
they do on the topic at hand. So far in this group the posters seem to
remember that we were all once newbies and are correspondingly helpful
and patient. I pray that it stays that way. It's refreshing. :-)

Nov 30 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

43
6880
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 performance. In the hopes of improving performance, we have tried to find a way to avoid the...
3
1796
by: Steve | last post by:
Hi, I have a class like: public ClassA { int vals1; int vals2; }
24
2620
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); ......
94
5678
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.
161
7844
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 other platform in every other language. Normally a value type is the actual data itself stored in...
0
9376
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9234
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9150
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9086
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6678
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4762
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3207
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2599
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2146
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.