This is from my post from another forum, about two years ago. Although the language is vb.net (unlike C #), the concepts of type type and link type are uniform throughout .net, and examples are still preserved.
It is also important to remember that inside .net all types are technically derived from the base type Object. Value types are intended to behave as such, but in the end they also inherit the functionality of the base Object type.
but. Value types are just that they represent a separate memory area in which the discrete value VALUE is stored. Value types have a fixed memory size and are stored on the stack, which is a set of addresses of a fixed size.
When you do this statement:
Dim A as Integer DIm B as Integer A = 3 B = A
You have done the following:
- Created 2 spaces in memory, sufficient to store 32-bit integer values.
- The value 3 is allocated in the memory allocation assigned to A
- The value 3 is allocated in the memory allocation assigned to B, assigning it the same value as in A.
The value of each variable exists discretely in each memory cell.
B. Types of links can be of different sizes. Therefore, they cannot be stored in "Stack" (remember that a stack is a collection of fixed-size memory allocations?). They are stored in a managed heap. Pointers (or "links") to each element of the managed heap are stored on the stack (as an address). Your code uses these pointers on the stack to access objects stored in the managed heap. Therefore, when your code uses a reference variable, it actually uses a pointer (or "address" in a memory location in a managed heap).
Suppose you create a class called clsPerson with the string Property Person.Name
In this case, when you do this statement:
Dim p1 As clsPerson p1 = New clsPerson p1.Name = "Jim Morrison" Dim p2 As Person p2 = p1
In the above case, the p1.Name property will return Jim Morrison, as you would expect. The p2.Name property will also return Jim Morrison, as you would expect. I believe that both p1 and p2 are separate addresses on the stack. However, now that you have assigned p2 to p1, both p1 and p2 point to SAME LOCATION on the managed heap.
Now SAVE THIS situation:
Dim p1 As clsPerson Dim p2 As clsPerson p1 = New clsPerson p1.Name = "Jim Morrison" p2 = p1 p2.Name = "Janis Joplin"
In this situation, you created one new instance of the Person class in the Managed Heap with a pointer p1 on the stack that references the object, and again set the Name property of the object instance to Jim Morrison. Then you created another pointer p2 on the stack and pointed it to that same address in the managed heap as on p1 (when you made the assignment p2 = p1).
Here is a twist. When you set the Name property the value of p2 to "Janis Joplin", you change the Name property of the object, LINKS AND AND p1 and p2, so if you run the following code:
MsgBox(P1.Name) 'Will return "Janis Joplin" MsgBox(p2.Name) 'will ALSO return "Janis Joplin"Because both variables (Pointers on the Stack) reference the SAME OBJECT in memory (an Address on the Managed Heap).
Did that make sense?
The last one. If you do THIS:
DIm p1 As New clsPerson Dim p2 As New clsPerson p1.Name = "Jim Morrison" p2.Name = "Janis Joplin"
You now have two different Person objects. However, as soon as you do IT again:
p2 = p1
Now you have both pointed to Jim Morrison. (I'm not quite sure what happened to the object on the heap referenced by p2 ... I THINK that it is now out of scope. This is one of those areas where, hopefully, someone can install me directly .. .). -EDIT: I BELIEVE, which is why you should set p2 = Nothing OR p2 = New clsPerson before doing a new job.
Once again, if you do IT now:
p2.Name = "Jimi Hendrix" MsgBox(p1.Name) MsgBox(p2.Name)
Both msgBoxes will return "Jimi Hendrix"
This can be quite confusing, and I will say for the last time that some details may be wrong.
Good luck, and hopefully others who know better than me will come to help clarify some of these.,