Re: Boxing and Unboxing ??
- From: "Peter Olcott" <NoSpam@xxxxxxxxxxxxx>
- Date: Sat, 13 Jan 2007 11:55:32 -0600
"Barry Kelly" <barry.j.kelly@xxxxxxxxx> wrote in message
news:2bphq2t6mbhudtjl0jl9j0frr49b1brsf6@xxxxxxxxxx
Peter Olcott wrote:
"Jesse McGrew" <jmcgrew@xxxxxxxxx> wrote:
[...]
Does that mean that you do have to call a method every time you add or
compare
two integers that are stored in reference types?
From the point of view of C#, an integer (or any other value type) is
only boxed if it's been assigned to a location of type 'object' -
whether local variable, argument or field.
Value types that are fields of a reference type are stored inline in the
memory for that object on the heap.
For example:
class A { int x; }
... can be imagined as being roughly equivalent (from a memory layout
perspective) to this in C:
typedef void *MethodTable; // CLR implementation detail
typedef struct A_ { MethodTable *mt; int x; } *A;
In fact, you can't add two boxed integers in C#, since it's got no way
to represent them as anything other than 'object'. You need to cast them
to 'int' to add them - and that unboxes them.
So a member function can not add two integer members without unboxing them
first? That would sound like horrendous design.
Example:
object x = 42; // x now contains a boxed int
object y = 10; // as does y
Console.WriteLine(x + y); // can't add object to object
int unboxedX = (int) x;
int unboxedY = (int) y;
Console.WriteLine(unboxedX + unboxedY); // etc.
You can view the assembly code in Visual Studio 2005. Run the program,
hit pause to break into the debugger, then right-click on a source line
and choose "Go to Disassembly".
Is that actual Intel machine specific assembly language, or the .NET virtual
machine assembly language?
Why don't you try it and see, before asking this kind of question?
It's the actual Intel machine code. Be aware of the usual gotchas re
Debug and Release mode.
You can get a higher-quality disassembly, with more correct CLR symbols,
with the MS symbol server (SRV* etc.) combined with SOS.DLL (or use
WinDbg with SOS).
-- Barry
--
http://barrkel.blogspot.com/
.
- Follow-Ups:
- Re: Boxing and Unboxing ??
- From: Barry Kelly
- Re: Boxing and Unboxing ??
- From: Bruce Wood
- Re: Boxing and Unboxing ??
- References:
- Boxing and Unboxing ??
- From: Peter Olcott
- Re: Boxing and Unboxing ??
- From: Jesse McGrew
- Re: Boxing and Unboxing ??
- From: Peter Olcott
- Re: Boxing and Unboxing ??
- From: Jesse McGrew
- Re: Boxing and Unboxing ??
- From: Peter Olcott
- Re: Boxing and Unboxing ??
- From: Barry Kelly
- Boxing and Unboxing ??
- Prev by Date: Re: Interprocess Communication
- Next by Date: Re: Boxing and Unboxing ??
- Previous by thread: Re: Boxing and Unboxing ??
- Next by thread: Re: Boxing and Unboxing ??
- Index(es):
Relevant Pages
|