Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ValueTypes

Status
Not open for further replies.

Laeg

Programmer
Nov 29, 2004
95
IE
The concept of ValueType vs Reference Type is understood along with the relation to Stack vs Heap but I have a question

int - a value type, stored on the Stack
int - inherits from System.ValueType which in turn is derived from System.Object

So why is int not considered an object and placed on the managed heap?

I have seen many explanations of this on the web and it is quite confusing. The summary of which is that

value types are not objects in the eyes of CLR at the point of allocation however boxing/unboxing is
completed on the fly as soon as a method (linked with their type) is called on them. So basically
what I read into that is that

Code:
int i; //Not considered an object at this point put on Stack

string s = i.ToString(); //i boxed, value assigned to s, i unboxed again

It still bodes the questions

Why is i not considered an object at
int i;
and put on the managed heap?
 
From what I understand primitive value types (int, bool, float, etc) are far more efficient because they are stored in 1 memory space and their values are dealt with directly.

There is no pointer to check first and the whole process of accessing the value is therefore faster.

Not sure if that's the whole story but it makes sense to me...

 
I know the theory of why it makes more sense to have primitive types as value types and stored on the stack. I'm just trying to understand what is going on (on the fly or otherwise) that

Code:
int i; //Not considered an object at this point put on Stack

can be true given that int derives from System.ValueType which in turn derives from System.Object.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top