From Troellsen's Professional c#:
"Given that .NET defines two major categories of types (value based and
reference based), you may occasionally need to represent a variable of
one category as a variable of the other category. C# provides a very simple
mechanism, termed boxing, to convert a value type to a reference type.
Assume that you have created a variable of type short:
// Make a short value type.
short s = 25;
If, during the course of your application, you wish to represent this
value type as a reference type, you would "box" the value as follows:
// Box the value into an object reference.
object objShort = s;
Boxing can be formally defined as the process of explicitly converting
a value type into a corresponding reference type by storing the variable
in a System.Object. When you box a value, the CLR allocates a new object
on the heap and copies the value type's value (in this case, 25) into that
instance. What is returned to you is a reference to the newly allocated
object. Using this technique, .NET developers have no need to make use
of a set of wrapper classes used to temporarily treat stack data as heap-allocated
objects. The opposite operation is also permitted through unboxing. Unboxing
is the process of converting the value held in the object reference back
into a corresponding value type on the stack. The unboxing operation begins
by verifying that the receiving data type is equivalent to the boxed type,
and if so, it copies the value back into a local stack-based variable.
For example, the following unboxing operation works successfully, given
that the underlying type of the objShort is indeed a short (you'll examine
the C# casting operator in detail in the next chapter, so hold tight for
now): // Unbox the reference back into a corresponding short.
short anotherShort = (short)objShort;"
I'll stop there due to my distaste for violating copyrights. You may wan
to pick up this book for your language jump. It's more about the language
and makes a lot of comparisons to c/c and Java.
Bob
>
>
>"Bob Graham" <(E-Mail Removed)> wrote in
>message news:(E-Mail Removed)...
>> Value types are stored on the "Stack" and go away, as it were,
>> immediately when they go out of scope. Mostly numeric types and structs.
>> Reference types are stored on the "Heap" and are garbage collected
>> when the system feels like it. References to ref types are passed
>> normally as a pointer to the address. Value types are passed a copy of the value.
>> I'm sure someone with more years under their Microsoft belt will
>> chime in here with a more exlicit and concise answer, but this is
>> basically how it is.
>> Bob
>
>What I am looking for is all of the extra steps that form what is
>referred to as boxing and unboxing. In C/C converting a value type to
>a reference type is a very simple operation and I don't think that
>there are any runtime steps at all.
>All the steps are done at compile time. Likewise for converting a
>reference type to a value type.
>
>in C/C
>int X = 56;
>int *Y = &X;
>Now both X and *Y hold 56, and Y is a reference to X;
>
>
>
>>
>>>
>>>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 other platform in every other language. Normally a value
>>>type is the actual data itself stored in memory, (such as an integer)
>>>and a reference type is simply the address of this data.
>>>
>>>It seems that .NET has made at least one of these two terms mean
>>>something entirely different. can someone please give me a quick
>>>overview of what the terms "value type" and "reference type" actually
>>>mean in terms of their underlying architecture?
>>>
>>>
>>>
>>>
>>
>>
>>
>> Posted by NewsLook (Trial Licence) from
>> http://www.ghytred.com/NewsLook/about.aspx
>>
>>
>>
>
>
>
>
Posted by NewsLook (Trial Licence) from
http://www.ghytred.com/NewsLook/about.aspx