473,473 Members | 1,800 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Basic types: objects or not?

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
14 2480
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
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
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
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
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
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

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
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
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
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
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: asfwa | last post by:
I'm new to C++ and I have some basic questions. I have written an app that does some network stuff in a worker thread. The thread function requests something from the server, gets it and creates...
3
by: p988 | last post by:
Learning C# is much tougher than I expected...Please help me by answering the following questions! Thank you in advance! 1. Are all Enumerations type Value type? 2. The line, RegistryKey...
2
by: Janus | last post by:
Hell This is a very basic question i know :O Formerly I developed in VisualBasic 6.0 and when a project is started you can select different project types. But what exactly is an ActiveX DLL? -...
21
by: Philipp | last post by:
Hey, did anyone have a good paper about the opject orianteted concept? wishes
5
by: kanchy kang | last post by:
Hi,all as we know, we can override the operator of one object(for example __eq__). my question is, how to override the basic operator? for example, for any object comparison operator(including...
4
by: Peter Morris [Droopy eyes software] | last post by:
Hi all, I have some basic WebService questions #1 Let's say I have a business class named "Customer". The customer would be able to log in and update their personal information like so void...
4
by: Schüle Daniel | last post by:
Hello, first question In : cmp("ABC",) Out: 1 against what part of the list is the string "ABC" compared? second question
3
by: Simon Hart | last post by:
Hi, I am trying to implement some functionality as seen in MS CRM 3.0 whereby a basic Xml is deserialized into an object which contains properties. What I want to do from here is; cast the basic...
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,...
0
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
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...

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.