Re: Boxing and Unboxing ??



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.

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: 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: 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: Accessing private data during finalize/dispose
    ... All data members of value types (int, char, double, enums, structs) are ... accessible, but reference types are ... code necessary to unmark the object but need to keep two strings and an int ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Web method signature modified.
    ... is 1 for int and 0 for string param and both I have tried and found it ... default value (null for reference types, ... However since the proxy is not with the latest ...
    (microsoft.public.dotnet.framework.aspnet.webservices)
  • Re: Detecting Intel / PowerPPC?
    ... executing on an Intel machine or a PowerPC? ... int CheckEndianness ... Byte *fooPrime; ...
    (comp.sys.mac.programmer.help)

Loading