473,548 Members | 2,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.ToSt ring();

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.WriteLi ne(squareRoot(N umberToRoot));

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 1753
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*****@hotmai l.com> a écrit dans le message de
news:48******** *************** ***@posting.goo gle.com...
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.ToSt ring();

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.WriteLi ne(squareRoot(N umberToRoot));

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.ValueTyp e. 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/
frlrfsystemvalu etypememberstop ic.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*****@hotmai l.com> wrote in message
news:48******** *************** ***@posting.goo gle.com...
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.ToSt ring();

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.WriteLi ne(squareRoot(N umberToRoot));

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******@clu b-internet.fr> wrote in message news:<O5******* *******@tk2msft ngp13.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*****@hotmai l.com> a écrit dans le message de
news:48******** *************** ***@posting.goo gle.com...
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.ToSt ring();

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.WriteLi ne(squareRoot(N umberToRoot));

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*****@hotmai l.com> a écrit dans le message de
news:48******** *************** ***@posting.goo gle.com...
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******@clu b-internet.fr> wrote in message

news:<O5******* *******@tk2msft ngp13.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*****@hotmai l.com> a écrit dans le message de
news:48******** *************** ***@posting.goo gle.com...
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.ToSt ring();

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.WriteLi ne(squareRoot(N umberToRoot));

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
323
by: Lora Connors | last post by:
What is Boxing & UnBoxing in .NET?
43
6848
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 another story.) The data is written once and then read many times. Each primitive read requires unboxing. The data reads are critical to overall app...
8
1715
by: Peter Olcott | last post by:
Exactly why does C# and .NET require all the extra copying of data?
14
5009
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 Object) Is this even possible, or is the composite pattern doomed to use the System.Object type forever? I have tried using interfaces with generics,...
94
5615
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. Thanks.
161
7741
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 biggest problem with this process is that the terms "value type" and "reference type" mean something entirely different than what they mean on every...
0
7518
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7711
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7954
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7805
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5085
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3478
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1932
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1054
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
755
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.