Re: Boxing and Unboxing ??



Peter Olcott wrote:
"Jesse McGrew" <jmcgrew@xxxxxxxxx> wrote in message
news:1168655360.762230.26860@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Well, if you're familiar with Delphi or Java, you've already seen
reference types. Class instances in those languages are always stored
as pointers to data on the heap, just like reference types in .NET, and
when you access an object's fields, you're implicitly deferencing the
pointer. In Delphi, records are equivalent to value types; in Java,
primitives like int and double are.

A value type is a type that's normally passed by value, and whose
contents *can* (but don't have to) live on the stack. A reference type
is always passed by reference, and always lives on the heap. A variable
of a value type takes up the entire size of the type, and assigning one
such variable to another copies the contents; a variable of a reference
type only takes up the size of a pointer, and assigning one to another
simply makes both variables point to the same data.

Boxing means copying a value type onto the heap, along with some type
information, so that it can be used like any other instance of
System.Object. This is because even though all types in .NET derive
from System.Object (a reference type), value types are stored
differently. To keep polymorphism and garbage collection working, the
data has to be copied at runtime, because you can't just use a pointer
to a value type on the stack as a managed reference - for example, you
might store that pointer in a global variable, where it would have to
live on after the function returns and its stack frame is destroyed.

Unboxing is the reverse - copying the contents of a boxed value type
(from the heap) back onto the stack so you can work with it in its
usual form.

Jesse

Well that is a little more clear now, thanks. So the "value types" have less
baggage? I try to understand these things in the same way that I understand
their equivalents in C and C++. I try to understand them in terms of the
underlying machine operations in assembly language.

Value types do have less baggage (in their unboxed form). For example,
int is a value type - you wouldn't want to have to dereference
pointers, call methods, etc. every time you added or compared two
integers. But they also have less functionality, because you can only
take full advantage of inheritance and polymorphism when you're using
reference types, just like you can only do it with pointers and
references in C++. You need reference types to get that kind of OOP
behavior, as well as to implement structures like trees and lists.

Take the following C# definitions:

struct Value {
public int foo;
}

class Ref {
public int bar;
}

Value my_val;
Ref my_ref;

my_val.foo = my_ref.bar = 0;

The equivalent in C++ would be:

class Value {
public:
int foo;
};

class Ref {
public:
int bar;
};

Value my_val;
Ref * my_ref;

my_val.foo = my_ref->bar = 0;

Every time you declare a variable or field of the type Ref, you're
really declaring a pointer; and when you call its methods or access its
fields, you still write "." in C#, but it works like "->".

With .NET this is a little trickier because it has another layer in-between, and
does not seem to be able to directly expose the actual platform specific
assembly language of what it is doing. In C or C++ I simply tell the compiler to
output assembly language, then I can see everything.

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

Jesse

.



Relevant Pages

  • Re: Boxing and Unboxing ??
    ... as pointers to data on the heap, just like reference types in .NET, and ... and always lives on the heap. ... type only takes up the size of a pointer, ... underlying machine operations in assembly language. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Get/Set sections of a class property confusing...
    ... Value types when used as ByRef ... For reference types however only the pointer is ... so in essence the original object (the true object ...
    (microsoft.public.dotnet.languages.vb)
  • Re: "Must instantiate controlled types at library level." Why?
    ... > Simple Mantra: 'Class behaves like String. ... C++ has a string class in its standard library. ... an array will "degrade" to a pointer to its ... C++ has reference types are equivalent to "in out." ...
    (comp.lang.ada)
  • Re: Delphi 7/8 Language enhancements
    ... > other types living on the heap, i.e. must be accessed by reference ... > which are not referenced through a pointer). ... reference types else value types? ...
    (borland.public.delphi.non-technical)
  • Re: Byref / Byval?
    ... There are two types of values in .NET: Value types and Reference types. ... For value types, ByRef and ByVal probably have the impact you think it should - if you pass it byref and change the value, it won't change in the calling method.. ... If you understand that any variable that holds a reference type is actually a pointer then...ByVal passes a copy of the pointer, so if you change any properties or call a method on the object, it WILL actually modify the object. ... In my mind, I see ByRef as passing a reference to an already created object, I can then use values of that object or do things to it, where-as ByVal creates a separate instance of it - if this is the case, presumably I now have 2 instances of this object which need to be destroyed - so presumably, not having to destroy two things is better? ...
    (microsoft.public.dotnet.framework.aspnet)

Loading