 Welcome to Value Types and Reference Types in the .NET Framework, Creatures of the Heap and Stack Dwellers. A stack is an in-memory data structure. Items are stored in a last-in, first-out, or LIFO manner. Consider a deck of playing cards. Stack data structures only allow for new cards to be placed on the top of the deck. When a card is removed or popped from the stack, it must be taken from the top of the deck. The last card pushed on the deck is the first card to be popped from the deck. This type of memory allocation is very efficient and requires very little overhead to manage. The heap, sometimes called the managed heap, is also an in-memory data structure. If we continue with a deck of playing cards analogy, the heap is more like a jumbled pile of cards. This seemingly chaotic pile is actually very flexible when it comes to storage. The heap is not restricted to the last-in, first-out rules like the stack. When we write software, we obviously aren't storing playing cards in memory. We are storing data. To understand how that data is stored, we have to look more closely at data types. Every variable we declare must have a data type. Integers, chars, booleans, and strings are all examples of data types, but there are other data types that are more complex. In fact, using classes allows you to create your own custom data types. The .NET framework classifies data types as either value types or reference types. Identifying which data types are value types and which are reference types will help us understand how the stack and heap work together to store the data we assigned to variable. Knowing whether a variable's value lives on the stack or on the heap will help us understand the behavior of the variables in our programs. In C-sharp, the value types are bool, byte, char, decimal, double, enum, float, int, long, as byte, short, struct, uint, ulong, and ushort. Value types are stack dwellers. When we store data in any of these types, the underlying value is stored on the stack. Let's compare the difference. We see that the value for both i and j are stored on the stack because they are value types. However, reference types require the stack to store the variable, a reference, and the heap to store the value, an object. This reference is a specialized mechanism that relies on pointers and memory addresses to track data on the heap from the stack. The following are C-sharp reference types, class, interface, delegate, object, and string. Reference types are creatures of the heap. The underlying value of reference types is stored on the heap. However, a reference to that storage location is kept on the stack. When we store data in any of these types, the value of that data is stored on the heap, and a reference to it is stored on the stack. When we use reference types, what is stored on the stack is a reference to the actual value, which is stored on the heap. You may be wondering, why do I need to know this? As you write code, it is important to understand how your data is stored because this affects code performance. Data stored on the heap behaves differently from data stored on the stack. Next, we will look at the effects of this dissimilar behavior. First, let's walk through an example that demonstrates how value types are stored in memory. Select the step into button. Consider the two integers we used earlier, i and j. When the integer variable i is created, the value of four is stored on the stack. When we create another variable, j, the value of 10, is also stored on the stack. When we assign i to be equal to j, what we are really saying is that i should be equal to the value of j, in this case, 10. Consider a person object instantiated from a person class. Select the debug button. We originally set the name value of my person to be Clark Kent. When we run this code, we see that the value is now Superman. Why did this happen? Now, click on the step into button to see the explanation. We originally set the name value of my person to be Clark Kent. We then instantiate another person object. Now, two person objects are on the heap, and each will have a reference on the stack pointing to that object. As soon as we set my person equal to my other person, both references point to the same person object on the heap, and will share whatever values are stored there. There is no longer a reference pointing to the first person object, and the next time the garbage collector runs, the object referenced by my person will be disposed of. It is important to understand how this behavior impacts reference types when they are passed as arguments in method calls. Let's look at another example. Now, click on the step into button to see how this differs from the last example. Now it's time to test your understanding. The variable and count is an example of A. The object LBL message is an example of A. The text property of the TXT first text boxes. An understanding of how value types and reference types are managed by the .NET framework and the Common Language Runtime or CLR will help us to write more efficient and bug free code. A clear grasp of the basic functions of the stack and heap will give us better insight into what is happening under the hood. You have completed value types and reference types in the .NET framework, creatures of the heap and stack dwellers.