Re: Boxing and Unboxing ??




"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/


.



Relevant Pages

  • Re: Boxing and Unboxing ??
    ... two integers that are stored in reference types? ... to 'int' to add them - and that unboxes them. ... Is that actual Intel machine specific assembly language, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Question about value types
    ... If you declare an int local variable in a method somewhere, ... Value types act in exactly the same way. ... Reference types act differently. ... An array of ints is allocated on ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Detecting Intel / PowerPPC?
    ... executing on an Intel machine or a PowerPC? ... int CheckEndianness ... Byte *fooPrime; ...
    (comp.sys.mac.programmer.help)
  • Re: Detecting Intel / PowerPPC?
    ... executing on an Intel machine or a PowerPC? ... int CheckEndianness ... rube-goldbergish solutions this question provokes. ...
    (comp.sys.mac.programmer.help)
  • Re: Detecting Intel / PowerPPC?
    ... executing on an Intel machine or a PowerPC? ... point numbers and then they need to be swapped (low / big endian) when they're ... int main ... bstevenson at remove this text dot apple dot com ...
    (comp.sys.mac.programmer.help)