473,320 Members | 1,910 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,320 software developers and data experts.

Question on boxing

Jay
I've been reading that boxing converts a value type to a reference type. If I understand this
correctly, the type on the heap is copied onto the stack.

This sound inefficient to me. Why doesn't C# just create a reference variable that points to the
object on the heap, which would act in a similar way to a true reference type?
Aug 8 '07 #1
5 1553
On Aug 8, 9:07 am, "Jay" <nospamwrote:
I've been reading that boxing converts a value type to a reference type. If I understand this
correctly, the type on the heap is copied onto the stack.

This sound inefficient to me. Why doesn't C# just create a reference variable that points to the
object on the heap, which would act in a similar way to a true reference type?
It's the other way around: the value on the stack (or inlined in
another object on the heap) is copied into a new object on the heap.

However, your question is still valid: why copy a value when you can
hold a pointer? The answer is that you can take the performance hit up
front, copying the value, or you can take it every time you use the
boxed value. That is, you can take a performance hit copying a value
(which, according to MS recommendations, should be small, no longer
than 16 bytes, so that it can be copied quickly), or you can take a
performance hit following a pointer over and over again.

I'd rather take the performance hit up front, personally, particularly
considering that it's nothing more than copying a few bytes from one
place to another.

Aug 8 '07 #2
Bruce Wood <br*******@canada.comwrote:
On Aug 8, 9:07 am, "Jay" <nospamwrote:
I've been reading that boxing converts a value type to a
reference type. If I understand this
correctly, the type on the heap is copied onto the stack.

This sound inefficient to me. Why doesn't C# just create a
reference variable that points to the
object on the heap, which would act in a similar way to a
true reference type?

It's the other way around: the value on the stack (or inlined in
another object on the heap) is copied into a new object on the heap.

However, your question is still valid: why copy a value when you can
hold a pointer? The answer is that you can take the performance hit up
front, copying the value, or you can take it every time you use the
boxed value. That is, you can take a performance hit copying a value
(which, according to MS recommendations, should be small, no longer
than 16 bytes, so that it can be copied quickly), or you can take a
performance hit following a pointer over and over again.

I'd rather take the performance hit up front, personally, particularly
considering that it's nothing more than copying a few bytes from one
place to another.
There's more to it than that.

Consider the case where you do:

object Return5()
{
int x = 5;
return x;
}

If it just returned a pointer to the original stack value then:

a) How would it know what type the value was meant to be?
b) What happens after the stack is popped at return time?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 8 '07 #3
Jon Skeet [C# MVP] wrote:
There's more to it than that.

Consider the case where you do:

object Return5()
{
int x = 5;
return x;
}

If it just returned a pointer to the original stack value then:

a) How would it know what type the value was meant to be?
Well, it'd have to store that information somewhere, of course. Maybe
you'd just change the language so that all stack frames include type
information for all variables in the frame, and the pointer wouldn't be
just to the variable data, but to that typed information describing the
variable data.

Alternatively, you could wrap the reference for the stack variable in
some sort of container. We could call it, oh...I don't know...a
"package"? No, too long. How about "carton". No, reminds me of
cigarettes. Maybe the word "box" would work?

:)
b) What happens after the stack is popped at return time?
Oh, I know! You just capture the variable, the way anonymous delegates
work. :)

My biggest issue with the idea of referencing the original variable is
that it means that the value could change externally to the boxed value,
and that change would be reflected in the user of the boxed type. This
is contrary to the normal behavior of value types. IMHO, one main point
of boxing is to wrap the value in something that can be used as a
reference type, but which retains the normal behavior of a value type.
If you just refer to the original value, which is available for changing
by other code elsewhere, then the boxed value type doesn't work like a
value type any more.

Pete
Aug 8 '07 #4
Hi,

"Jay" <nospamwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
I've been reading that boxing converts a value type to a reference type.
If I understand this
correctly, the type on the heap is copied onto the stack.
It's the opposite, it's copied from the stack to the heap, but is not
precisely that, the value is copied but then you get a reference to it
through an instance of Object class.
This sound inefficient to me. Why doesn't C# just create a reference
variable that points to the
object on the heap, which would act in a similar way to a true reference
type?
It's a problem of how long the variable will "live" , if you only keep it in
the stack, as soon as that stack frame is released the variable is lost.
Wether in the heap it can live for as long as it's referenced.
Aug 8 '07 #5
Jay
Thanks for your replies everyone. I'm still lacking enough knowledge to fully understand the
reasons, but it's now clear to me that it's not as simple as I first thought!

Jay

"Jay" <nospamwrote in message news:%2****************@TK2MSFTNGP06.phx.gbl...
I've been reading that boxing converts a value type to a reference type. If I understand this
correctly, the type on the heap is copied onto the stack.

This sound inefficient to me. Why doesn't C# just create a reference variable that points to the
object on the heap, which would act in a similar way to a true reference type?

Aug 9 '07 #6

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...
2
by: Eric Newton | last post by:
Since String.Format has to box all value types to accomodate the params, and just for sheer efficiency, are there possibly any plans for a FormatValue method to minimize boxing? public static...
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...
16
by: Ed A | last post by:
Hi all: I'm using a struct of type Point that is being passed on to a method as a refrence type (boxing), the UpdatePoint method changes the coordinates of this point as such: Point p = new...
10
by: Nathan Neitzke | last post by:
I am writing an app that might largely benefit from using a struct instead of a class. However, it needs to be rich enough where there are methods available. My question is - every time you...
2
by: Mike D Sutton | last post by:
Please excuse the terribly 'newbie'ness of this question, unfortunately my C# is very rusty.. What I'm trying to do is write a simple interactive drawing application where a few lines can be moved...
20
by: Marcel Hug | last post by:
Hi NG! In my book I have the following code simple and I tested it: public class Base : ICloneable { public int Age; public string Name; public Base(string myname)
2
by: Mr Flibble | last post by:
Is boxing using object the same as the use of variant in VB? Is it used to de-typify a variable, which must then later be cast back to it's original type?
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...
20
by: tshad | last post by:
Using VS 2003, I am trying to take a class that I created to create new variable types to handle nulls and track changes to standard variable types. This is for use with database variables. This...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
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...
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: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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)...
0
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: 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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.