473,386 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Boxing and data types

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.
Nov 15 '05 #1
5 1742
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.

"Craig" <cr*****@hotmail.com> a écrit dans le message de
news:48**************************@posting.google.c om...
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.

Nov 15 '05 #2
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/de...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/de...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
"Craig" <cr*****@hotmail.com> wrote in message
news:48**************************@posting.google.c om...
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.



Nov 15 '05 #3
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.

"Bruno Jouhier [MVP]" <bj******@club-internet.fr> wrote in message news:<O5**************@tk2msftngp13.phx.gbl>...
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.

"Craig" <cr*****@hotmail.com> a écrit dans le message de
news:48**************************@posting.google.c om...
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.

Nov 15 '05 #4
Hi Craig,

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

Take care,

Bruno.

"Craig" <cr*****@hotmail.com> a écrit dans le message de
news:48**************************@posting.google.c om...
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.

"Bruno Jouhier [MVP]" <bj******@club-internet.fr> wrote in message

news:<O5**************@tk2msftngp13.phx.gbl>...
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.

"Craig" <cr*****@hotmail.com> a écrit dans le message de
news:48**************************@posting.google.c om...
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.

Nov 15 '05 #5
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
Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Lora Connors | last post by:
What is Boxing & UnBoxing in .NET?
43
by: Mountain Bikn' Guy | last post by:
I have a situation where an app writes data of various types (primitives and objects) into a single dimensional array of objects. (This array eventually becomes a row in a data table, but that's...
8
by: Peter Olcott | last post by:
Exactly why does C# and .NET require all the extra copying of data?
14
by: dave.dolan | last post by:
Basically I'd like to implement the composite design pattern with leaves that are either of reference or value types, but even using generics I can't seem to avoid boxing (using ArrayList or...
94
by: Peter Olcott | last post by:
How can I create an ArrayList in the older version of .NET that does not require the expensive Boxing and UnBoxing operations? In my case it will be an ArrayList of structures of ordinal types. ...
161
by: Peter Olcott | last post by:
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.