Question Value Types and the Stack or Heap

spurlock

New member
Joined
Dec 10, 2008
Messages
3
Programming Experience
3-5
Can someone please help me to understand this.

Yes I understand value types on the stack and ref types on the heap. I understand Integer is a value type. I understand that when auto-boxed it is placed on the heap and when unboxed it is placed on the stack.

How does this work when calling a method of the Integer class?
e.g.

dim x as Integer = 10 'This is on the Stack
x.CompareTo(100) 'what happens here are value types allowed to have
'methods?
 
Integer data type is represented in .Net by the Int32 structure. Yes, structures can have methods.
 
Sorry, I guess I do understand value types have methods, but are those methods located within an object stored on the stack or heap?
 
I belive you answered that in your first post, structures are value types, which is stack/inline.
 
Ok, but what happens when I call a method on my instance of the Integer class? Does that need to be autoboxed and put on the heap? Or does that object and its accompanying methods live on the stack?
 
Value types only goes on Heap if they are boxed in a Object type reference, or is declared at class level within a reference type. Calling a value type method on an instance declared within a method does not box it and replace it to my knowledge.
 
Back
Top