Boxing and data types

C

Craig

Hi everyone,

As a relative new-comer to the wonderful world of .NET Framework and
the C# langauge I have come across something that I would like to
clarify (hopefully with the help of kind people such as yourselves).

To quote, "Everything in C# is an object" (including "primitive" value
types, primitive being in quotations because this is what my question
concerns).

According to the material that I have studied it is possible to define
a variable to be the size of 1 byte:

byte myVariable = 245;

Remembering that 1 byte is just large enough to hold values in the
range of 0-255. How then is it possible to invoke similar to this...

myVariable.ToString();

How can the variable 'myVariable' be large enough to hold the data
members derived from System.Object, if it is only large enough to hold
the largest possible 'byte' value (255)? Where does the extra space
come from???
I have been through a lot of reference material before asking this
question and I cannot find anything to describe this situation. All
that I can assert is that when the method ToString() (or any other
'Object' derived method) is invoked on 'myVariable', this variable
('myVariable') is automatically 'boxed' behind the scenes. Thus,
providing the data members and methods associated with System.Object.

My question also stands for 'ints'.

E.g. 32767.ToString(). Is this automatically boxed too?

Also, Is this what happens when a value type (e.g. 245) is passed into
a function (that expects a value type) like so....

int NumberToRoot = 245;
Console.WriteLine(squareRoot(NumberToRoot));

Or, is it that only the value 245 is copied in to the function and
whenever the function perfoms any operations on the number (e.g.
ToString()) is it that the value gets boxed (behind the scenes)? Or...
am I completely missing the point? :)

I apologise to anyone for being a little stupid beforehand and I
appreciate any help that is offered.

Thanks in advance.

Kind regards,

Craig.
 
B

Bruno Jouhier [MVP]

Craig,

First, the Object class does not define any data members, just methods. So,
all you need in an object is some kind of class pointer (that contains the
virtual table) + probably a few bytes of data for memory management/garbage
collection and synchronization (every object holds a monitor). Of course,
this is a lot more than 1 byte (probably somewhere between 8 and 16 bytes).

So, when you "box" a byte value, you get an object that takes a few bytes.
But when you manipulate the byte value in contexts where the compiler knows
its type exactly (the fact that it is a byte, and not some instance of some
class that derives from object), the value is not boxed and it only takes 1
byte.

So, if you write:

byte b = 245;
string s = b.ToString();

The compiler "knows" that b is a byte and it also knows that the ToString()
method that you call is the one defined by the System.Byte class (because
System.Byte is sealed). So, it does not need to "box" your byte, it can pass
the 1 byte value directly to the function that implements the ToString
method.

So, the notation may look object-oriented and you may have the impression
that the value will be boxed, with all the overhead that this implies, but
in contexts like this one, you won't get any boxing and the compiler will
translate it into a simple function call.

On the other hand, if you write:

object obj = b;
string s = obj.ToString();

Then, obj will be the "boxed" version of your byte and the ToString() method
call will go through a virtual table indirection.

Bruno.
 
G

Garrett

Hi Craig,

Value types, such as int, implicitly inherit from the
class System.ValueType. This seems to say everything
is a class, but some classes are value-types while others
are reference types.

see:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csspec/html
/vclrfcsharpspec_4_3.asp

As such they can access any class member (no boxing).
For a list of members see:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemvaluetypememberstopic.asp


If you want to see if a particular use of an int
involves boxing, you can always compile and use ildasm.

Now just what is going on? The value types deal with
the actual bits of data while a reference type is like a pointer
to the bits. So when a pointer (ref type) is expected the
value type has to be converted (boxed) to a reference type.

Boxing involves putting the "boxed stuff" on the heap.

It may be more helpful to think value-type/reference-type
rather than is it an object.

I don't know a clean reference to all this. No doubt there
is one.

Have a look at Tom Archer's book, Inside C#, pp 59-60.
But be aware he uses the word "magic" which may not
the most helpful word to use in trying to explain what
is going on.

Poke around msdn.microsoft.com. You can probably
piece together a better picture.

I hope this helps or at least doesn't make things murkier.
I'm not the most comfortable with the explanations I've
seen either.

Garrett
 
C

Craig

Hi Bruno,

Thank you very much. I was not expecting an answer so soon. :)

I found your answer very informative and clear. You have kindly
clarified that issue for me. (The compiler takes care of it).

Thanks again.

Kind regards,

Craig.
 
B

Bruno Jouhier [MVP]

Hi Craig,

I'm glad it helped. Thanks for the nice thank you note (we don't always get
them :<)

Take care,

Bruno.
 
E

Evan Stone

Hello Craig,

In a nutshell, here's a description of the process from Dan Appleman's
"Moving to VB.NET..." (which applies to C#.NET as well):

- Value types (i.e. primitive data types and structure objects) are stored
on the stack.
- Reference types are stored on the heap.

When the .NET runtime needs to access a Value type as a reference, it
performs Boxing in which an object is allocated on the heap and all the
values are copied on to the newly allocated heap object (the opposite is
called Unboxing).

Hope this helps!

-Evan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top