By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,829 Members | 1,829 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,829 IT Pros & Developers. It's quick & easy.

Basic types: objects or not?

P: n/a
Are basic types (int, long, ...) objetcs or not?

I read that in C# all are objects including basic types, derived from Object
class. Then in msdn documentation says that boxing converts basic types in
objects. But if they are objects why itīs need this conversion? Arenīt
objects (basic types) like Java?

Nov 17 '05 #1
Share this Question
Share on Google+
14 Replies


P: n/a
Primitive types in .Net such as int, float etc are aliases for system objects
such as System.Int32.

In .Net there are two main kinds of types value types and reference types.
A value type is a type where the actual value is stored inside the area of
memory pointed to by the variable:

i.e.
int a = 3

means that "a" is an alias for a memory location which is a 32 bit space
containing the value 3. Value types are stored on the stack (one of the
types of areas of memory allocated to a process), the stack also stores
function parameters when you make a function call and return pointers to
allow the program to know where it needs to jump back to after a function
call.

You will actually see that for a variable of type int, it has the alias
System.Int32, which derives from System.ValueType which derives from
System.Object.

The stack is not always suitable for some kinds of variables because it is
not very persistent, it is always changing and the scope of items on it only
last for the procedure call tree, so another type of variable is needed.

The other main type are reference types. These are objects that do not live
on the stack but in the managed heap, an area of memory that allows variables
to be stored for the entire program execution. Objects living in the heap
can be of any size. This time when you have a variable pointing to an object
the variable does not point directly to the object but it contains the memory
address of the object on the heap

i.e.
MyObject x -> contains -> 32bit number which points to -> address of x on
the heap

You will see that value type can not be null (because they do not point to
memory locations) they must have a value becuase they just store data. So
int x = null is not valid whereas int x = -1 is.
The final piece of the puzzle is boxing/unboxing, the process of boxing is
to turn a value type into a reference type so it can be treated as an object,
unboxing is the opposite process. A simple reason why we may want to do this
is:

int i =3;
i.ToString();

in this case the int is a value type but we want to treat it like an object
with a method called ToString(), in the code above i is being boxed.

When you box one important thing to keep in mind is that the actual value of
the value type is COPIED into the boxed object, and when it is unboxed it is
COPIEd again out of the object, so for example:

int i =1;
object obj = (object)i;
i = i +1;
int j = (int)obj;

print(i);
print(j);

You will see the output 2 : 1 (maybe some people would expect 2 : 2)

This is because when you do object obj = (object)i the value inside is
copied into the obj object when the boxing ocurrs.
Hope this helps answer your question.
Mark.
"luis" wrote:
Are basic types (int, long, ...) objetcs or not?

I read that in C# all are objects including basic types, derived from Object
class. Then in msdn documentation says that boxing converts basic types in
objects. But if they are objects why itÂīs need this conversion? ArenÂīt
objects (basic types) like Java?

Nov 17 '05 #2

P: n/a
Thanks. I understand the example and your explications but my doubt
continue: are basic types (int, long) objects?

Reasons to be objects:
- i read that alll in C# are objects (derived from Object) including basic
types

Reasons not to be objects:
- msdn says that boxing converts basic type in Object
- this variables stored the valor , nor a reference, like basic types in
others languages (like C or java)

"luis" <as*****@jazzfree.com> escribió en el mensaje
news:ud**************@TK2MSFTNGP10.phx.gbl...
Are basic types (int, long, ...) objetcs or not?

I read that in C# all are objects including basic types, derived from Object class. Then in msdn documentation says that boxing converts basic types in
objects. But if they are objects why itīs need this conversion? Arenīt
objects (basic types) like Java?

Nov 17 '05 #3

P: n/a
Mark R. Dawson <Ma*********@discussions.microsoft.com> wrote:

<snip>
Value types are stored on the stack (one of the
types of areas of memory allocated to a process), the stack also stores
function parameters when you make a function call and return pointers to
allow the program to know where it needs to jump back to after a function
call.


Stacks aren't only stored on the stack, and this description has caused
numerous people problems in the past. See
http://www.pobox.com/~skeet/csharp/memory.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4

P: n/a
Hi, Luis,

I think Mark gave out a very good explanation of what is value
type/reference type. I think not many people can explain this better
than him.

Now comes to your questions, as long as you understand how it works, why
do you need stick to certain unclear definitions. I mean, the way you
think whether it is object or not depends on how you define the object,
which you already know there are different understandings.

Understand how it works is more important than how it defines.

HTH

luis wrote:
Thanks. I understand the example and your explications but my doubt
continue: are basic types (int, long) objects?

Reasons to be objects:
- i read that alll in C# are objects (derived from Object) including basic
types

Reasons not to be objects:
- msdn says that boxing converts basic type in Object
- this variables stored the valor , nor a reference, like basic types in
others languages (like C or java)

"luis" <as*****@jazzfree.com> escribió en el mensaje
news:ud**************@TK2MSFTNGP10.phx.gbl...
Are basic types (int, long, ...) objetcs or not?

I read that in C# all are objects including basic types, derived from


Object
class. Then in msdn documentation says that boxing converts basic types in
objects. But if they are objects why itīs need this conversion? Arenīt
objects (basic types) like Java?


Nov 17 '05 #5

P: n/a
luis <as*****@jazzfree.com> wrote:
Are basic types (int, long, ...) objetcs or not?
No, they aren't.
I read that in C# all are objects including basic types, derived from Object
class. Then in msdn documentation says that boxing converts basic types in
objects. But if they are objects why itīs need this conversion? Arenīt
objects (basic types) like Java?


They aren't objects (either in C# or in Java). However, in C# (or
rather, .NET in general) there is a reference type for every value type
- the two types have the same name, but the reference type is a
"boxed" version of the value type. The value type itself doesn't derive
from anything, nor can it be derived from. When the need arises to
treat it as a reference type, or implementing an interface, the value
is boxed - an instance of the reference type is created on the heap,
and used instead.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #6

P: n/a
Hi John,
that is a really excellent article. So when static value types are
allocated are they boxed and stored on the heap, or just stored in the heap
as a value?

Thanks
Mark.

"Jon Skeet [C# MVP]" wrote:
Mark R. Dawson <Ma*********@discussions.microsoft.com> wrote:

<snip>
Value types are stored on the stack (one of the
types of areas of memory allocated to a process), the stack also stores
function parameters when you make a function call and return pointers to
allow the program to know where it needs to jump back to after a function
call.


Stacks aren't only stored on the stack, and this description has caused
numerous people problems in the past. See
http://www.pobox.com/~skeet/csharp/memory.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #7

P: n/a

They aren't objects (either in C# or in Java). However, in C# (or
rather, .NET in general) there is a reference type for every value type
- the two types have the same name, but the reference type is a
"boxed" version of the value type. The value type itself doesn't derive
from anything, nor can it be derived from. When the need arises to
treat it as a reference type, or implementing an interface, the value
is boxed - an instance of the reference type is created on the heap,
and used instead.
-----------------

You are saying boxing implicitly occurs on primative types only when I do:
int i = 1;
string iString = i.ToString();

or call some available method for i?

Is the above more efficient than casting:
int i = 1;
string iString = (string)i;

Does it implicitly unboxe at any point? The last line of this code for
example:
int i = 1;
string iString = i.ToString();
int j = i

Thanks,
Brett
Nov 17 '05 #8

P: n/a
In the .NET Framework, all types derive from System.Object, which could be
lovingly referred to as "the Mother Function".
What exactly type of "conversion" do you require to do?
-Peter

"luis" <as*****@jazzfree.com> wrote in message
news:ud**************@TK2MSFTNGP10.phx.gbl...
Are basic types (int, long, ...) objetcs or not?

I read that in C# all are objects including basic types, derived from
Object
class. Then in msdn documentation says that boxing converts basic types in
objects. But if they are objects why itīs need this conversion? Arenīt
objects (basic types) like Java?

Nov 17 '05 #9

P: n/a
Mark R. Dawson <Ma*********@discussions.microsoft.com> wrote:
that is a really excellent article. So when static value types are
allocated are they boxed and stored on the heap, or just stored in the heap
as a value?


I believe they're stored on the heap as a value, as if the type itself
had an object containing the static variables.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #10

P: n/a
Brett <no@spam.net> wrote:
They aren't objects (either in C# or in Java). However, in C# (or
rather, .NET in general) there is a reference type for every value type
- the two types have the same name, but the reference type is a
"boxed" version of the value type. The value type itself doesn't derive
from anything, nor can it be derived from. When the need arises to
treat it as a reference type, or implementing an interface, the value
is boxed - an instance of the reference type is created on the heap,
and used instead.
-----------------

You are saying boxing implicitly occurs on primative types only when I do:
int i = 1;
string iString = i.ToString();

or call some available method for i?
In fact, there's no boxing needed for that, because the Int32 class
overrides ToString. If you had your own value type which didn't
override ToString, however, calling ToString would involve boxing the
value first.
Is the above more efficient than casting:
int i = 1;
string iString = (string)i;
That doesn't actually work - the int isn't a string, and there's no
conversion between them as far as the language is concerned.
Does it implicitly unboxe at any point? The last line of this code for
example:
int i = 1;
string iString = i.ToString();
int j = i


There's no boxing involved there.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #11

P: n/a
Peter Bromberg [C# MVP] <pb*******@yahoo.com> wrote:
In the .NET Framework, all types derive from System.Object, which could be
lovingly referred to as "the Mother Function".


Not quite - all *object* types derive from System.Object. Interface
types and value types don't.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #12

P: n/a
"luis" <as*****@jazzfree.com> wrote in
news:ud**************@TK2MSFTNGP10.phx.gbl:
Are basic types (int, long, ...) objetcs or not?
Yes and no. They act like them, but internally are not unless forced to be in which case they are
boxed.
I read that in C# all are objects including basic types, derived from
Object class. Then in msdn documentation says that boxing converts
basic types in objects. But if they are objects why it s need this
conversion? Aren t objects (basic types) like Java?


Because they arent really objects. They are internally primitive types for performance reasons.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Make your ASP.NET applications run faster
http://www.atozed.com/IntraWeb/
Nov 17 '05 #13

P: n/a
Static variables are stored on a CLR (per application domain) private heap,
called the "High Frequency" Heap as in Jon's article.
The static variables do contain the actual values, that means an int
variable is stored as a 32bit entity.
Boxing a static variable is just like boxing an instance variable, the value
is wrapped in an object of that type and stored on the GC heap.
Consider following code :

static int i = 255;
void SomeMethod() {
Object o = i; //boxing operation
...

If we watch the heaps and the stack after the assignment, we'll see this
(the addresses are fictive)

High Freq. Heap
address value
00cd0068 000000FF

|
box
|
V

GC Heap
address value
01282000 00000000 Sync. # Synch. block index
01282004 790ec308 Type Handle pointer to Method Table for
System.Int32 type
01282008 000000FF value

stack (of current thread)

0012f420 01282004 o -----> Reference to object o of type System.Int32

What we see is that the static variable contains the actual value but
doesn't carry any type information.
The boxed value is GC heap allocated object of type System.Int32, pointed to
by a GC tracked reference variable o on the stack.

Willy.

"Mark R. Dawson" <Ma*********@discussions.microsoft.com> wrote in message
news:DE**********************************@microsof t.com...
Hi John,
that is a really excellent article. So when static value types are
allocated are they boxed and stored on the heap, or just stored in the
heap
as a value?

Thanks
Mark.

"Jon Skeet [C# MVP]" wrote:
Mark R. Dawson <Ma*********@discussions.microsoft.com> wrote:

<snip>
> Value types are stored on the stack (one of the
> types of areas of memory allocated to a process), the stack also
> stores
> function parameters when you make a function call and return pointers
> to
> allow the program to know where it needs to jump back to after a
> function
> call.


Stacks aren't only stored on the stack, and this description has caused
numerous people problems in the past. See
http://www.pobox.com/~skeet/csharp/memory.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #14

P: n/a
Thanks to all

"luis" <as*****@jazzfree.com> escribió en el mensaje
news:ud**************@TK2MSFTNGP10.phx.gbl...
Are basic types (int, long, ...) objetcs or not?

I read that in C# all are objects including basic types, derived from Object class. Then in msdn documentation says that boxing converts basic types in
objects. But if they are objects why itīs need this conversion? Arenīt
objects (basic types) like Java?

Nov 17 '05 #15

This discussion thread is closed

Replies have been disabled for this discussion.