PC Review


Reply
Thread Tools Rate Thread

Boxing and Unboxing ??

 
 
Peter Olcott
Guest
Posts: n/a
 
      12th Jan 2007
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?


 
Reply With Quote
 
 
 
 
Bob Graham
Guest
Posts: n/a
 
      12th Jan 2007
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

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



 
Reply With Quote
 
Peter Olcott
Guest
Posts: n/a
 
      12th Jan 2007

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



 
Reply With Quote
 
Bob Graham
Guest
Posts: n/a
 
      12th Jan 2007
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



 
Reply With Quote
 
Bob Graham
Guest
Posts: n/a
 
      12th Jan 2007
But Generics are a more powerful alternative that you may want to read
up on. They get rid of boxing and unboxing penalties.
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



 
Reply With Quote
 
Dave Sexton
Guest
Posts: n/a
 
      13th Jan 2007
Hi Bob,

> Value types are stored on the "Stack" and go away, as it were, immediately
> when they go out of scope.


They can also be stored on the heap when they are fields of an object, for
instance. I like to think of value types as being in-line in terms of
memory. In other words, they can live anywhere since it's their "value"
that's important. On the contrary, reference types must live somewhere
where their "reference" can be used - the heap in .NET.

> Mostly numeric types and structs.


If you include enums then you've named them all, although they are all
really structures (structs, if you want to use the term loosely). A value
type in the .NET framework is any object that derives from
System.ValueType. The C# compiler, though, requires you to specify the
struct keyword instead of class, but that just means your class derives from
System.ValueType.

<snip>

--
Dave Sexton
http://davesexton.com/blog


 
Reply With Quote
 
Dave Sexton
Guest
Posts: n/a
 
      13th Jan 2007
Hi Peter,

> 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?


Your definitions are correct even in .NET. The real difference between the
framework and some of the other platforms you may be accustomed to is in the
management of memory. i.e., garbage collection.

--
Dave Sexton
http://davesexton.com/blog


 
Reply With Quote
 
Peter Olcott
Guest
Posts: n/a
 
      13th Jan 2007

"Bob Graham" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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;"


So a reference type is not anything at all like what the term "reference type"
means everywhere outside of the .NET. architecture. They probably should have
chosen different names such as Managed Heap Type and Stack Type, this would have
been far less misleading.

What I really want to see is the underlying architecture of Managed Heap Type
and Stack Type. In particular is there a whole lot of extra baggage for this
"value type" (Stack Type) as there seems to be for the Managed Heap Type
(reference type) ???

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



 
Reply With Quote
 
Peter Olcott
Guest
Posts: n/a
 
      13th Jan 2007
So with Generics Boxing and UnBoxing beomes obsolete?

"Bob Graham" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> But Generics are a more powerful alternative that you may want to read
> up on. They get rid of boxing and unboxing penalties.
> 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
>
>
>



 
Reply With Quote
 
Peter Olcott
Guest
Posts: n/a
 
      13th Jan 2007

"Dave Sexton" <dave@jwa[remove.this]online.com> wrote in message
news:%235C%23%(E-Mail Removed)...
> Hi Peter,
>
>> 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?

>
> Your definitions are correct even in .NET. The real difference between the
> framework and some of the other platforms you may be accustomed to is in the
> management of memory. i.e., garbage collection.


It seems that .NET adds a whole lot of extra baggage to these otherwise very
simple terms.
int X = 56; // refers to 56 (value type)
int* Y = &X; // Y refers to the address of 56 (reference type)
That is all there is to it, no runtime cost involved at all, no complex
underlying infrastructure.

>
> --
> Dave Sexton
> http://davesexton.com/blog
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Boxing and UnBoxing Joe Microsoft C# .NET 5 3rd Apr 2006 05:51 PM
Boxing/unboxing Jonathan Lurie Microsoft Dot NET Framework Forms 2 19th Oct 2005 10:17 AM
Boxing / Unboxing Steve Microsoft C# .NET 3 24th May 2004 10:16 AM
Boxing & Unboxing F1 F1 F1... Justine Microsoft C# .NET 6 3rd Dec 2003 09:35 AM
Boxing and UnBoxing VB.NET vs C# Andy Read Microsoft Dot NET 3 5th Sep 2003 06:29 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:44 AM.