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