473,516 Members | 3,488 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Boxing & Unboxing F1 F1 F1...

Hi All,

I need a small clarification with regard to Boxing.

struct Currency
{ ......... }

Currency Bal = new Currency;
Object Obj = Bal;

i read that when implicit boxing is taking place, ther
will be an hidden class (Reference Type) of type Currency
created internal by .NET Framework (to which direct
access is not easy). And when actual boxing is taking
place the object of the hidden class gets instanciated
and reference is store in "Obj".
My Doubt:- Does this mean that there will be 2 copies of
the data on RAM (One for Value Type ie struct Currency
and other for Hidden Class. if yes then isn't it
overhead?). Kindly help me in understanding this.

Thanz in Advance...
Justine
Nov 15 '05 #1
6 1257
Justine <am***********@hotmail.com> wrote:
I need a small clarification with regard to Boxing.

struct Currency
{ ......... }

Currency Bal = new Currency;
Object Obj = Bal;

i read that when implicit boxing is taking place, ther
will be an hidden class (Reference Type) of type Currency
created internal by .NET Framework (to which direct
access is not easy). And when actual boxing is taking
place the object of the hidden class gets instanciated
and reference is store in "Obj".
Yup.
My Doubt:- Does this mean that there will be 2 copies of
the data on RAM (One for Value Type ie struct Currency
and other for Hidden Class. if yes then isn't it
overhead?). Kindly help me in understanding this.


Well, there'll be two copies while the value type variable is still in
scope, yes. The fact that the object can live on outside the scope of
the variable which initially refers to it means that there *has* to be
a separate copy, really. I'm not sure whether or not I'd call that
overhead, but certainly boxing/unboxing is something you should always
be aware of when thinking about your types.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
Thanz Jon :-)

Justine
-----Original Message-----
Justine <am***********@hotmail.com> wrote:
I need a small clarification with regard to Boxing.

struct Currency
{ ......... }

Currency Bal = new Currency;
Object Obj = Bal;

i read that when implicit boxing is taking place, ther
will be an hidden class (Reference Type) of type Currency created internal by .NET Framework (to which direct
access is not easy). And when actual boxing is taking
place the object of the hidden class gets instanciated
and reference is store in "Obj".
Yup.
My Doubt:- Does this mean that there will be 2 copies of the data on RAM (One for Value Type ie struct Currency
and other for Hidden Class. if yes then isn't it
overhead?). Kindly help me in understanding this.


Well, there'll be two copies while the value type

variable is still inscope, yes. The fact that the object can live on outside the scope ofthe variable which initially refers to it means that there *has* to bea separate copy, really. I'm not sure whether or not I'd call thatoverhead, but certainly boxing/unboxing is something you should alwaysbe aware of when thinking about your types.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
.

Nov 15 '05 #3
Hi Jon forgive my ignorance here (im a c# newbie), but I have a further
question...
When boxing a value type, a temporary reference is created to allow it
to be treated as a reference type for instance..

int I = 100;
object O;
O = I;

So that if my understanding is correct, line1 creates the integer I on
the stack, line two creates an object reference pointer on the stack
and line three causes the object O reference pointer to point
(indirectly) to the integer I.
Would I be right in thinking that since when you instanciate a new
object instance it creates the object on the heap, in this case does
somekind of object (which is presumably the object box) get created on
the heap which references the stack int I?

in simpler terms....

STACK HEAP
O(objref)---------->I(Obj box which points to)
|
|
I(Integer) <--------

Thanks in advance,

Br,

Mark.

--

Br,
Mark Broadbent
mcdba , mcse+i
=============
Nov 15 '05 #4
Mark Broadbent <no***************************@virgin.net> wrote:
Hi Jon forgive my ignorance here (im a c# newbie), but I have a further
question...
When boxing a value type, a temporary reference is created to allow it
to be treated as a reference type for instance..

int I = 100;
object O;
O = I;

So that if my understanding is correct, line1 creates the integer I on
the stack, line two creates an object reference pointer on the stack
and line three causes the object O reference pointer to point
(indirectly) to the integer I.
Line three causes the object O reference pointer to point to a *new*
object, which has the current value of I.
Would I be right in thinking that since when you instanciate a new
object instance it creates the object on the heap, in this case does
somekind of object (which is presumably the object box) get created on
the heap which references the stack int I?


It doesn't reference the stack at all - it's a copy, effectively. So if
you change the value of I afterwards, that wouldn't change the object O
refers to at all.

Hope that helps.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5
Mark,

Just to add to Jon's reply...suppose you were to pass the
object reference you created in line 2 off to some other
asynchronous method that might not get to using it before
your initial function returns. If the reference were
pointing back to the stack someplace that value would now
be gone...

JM
-----Original Message-----
Hi Jon forgive my ignorance here (im a c# newbie), but I have a furtherquestion...
When boxing a value type, a temporary reference is created to allow itto be treated as a reference type for instance..

int I = 100;
object O;
O = I;

So that if my understanding is correct, line1 creates the integer I onthe stack, line two creates an object reference pointer on the stackand line three causes the object O reference pointer to point(indirectly) to the integer I.
Would I be right in thinking that since when you instanciate a newobject instance it creates the object on the heap, in this case doessomekind of object (which is presumably the object box) get created onthe heap which references the stack int I?

in simpler terms....

STACK HEAP
O(objref)---------->I(Obj box which points to)
|
|
I(Integer) <--------

Thanks in advance,

Br,

Mark.

--

Br,
Mark Broadbent
mcdba , mcse+i
=============
.

Nov 15 '05 #6


--

Br,
Mark Broadbent
mcdba , mcse+i
=============
Nov 15 '05 #7

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

Similar topics

43
6844
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...
3
1789
by: Steve | last post by:
Hi, I have a class like: public ClassA { int vals1; int vals2; }
24
2575
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); ......
16
1604
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 Point(); UpdatePoint( P ); .. ..
94
5580
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
13684
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
7702
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
7182
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
7574
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...
0
5712
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
5106
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...
0
3265
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3252
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1620
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
1
823
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
487
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.