Skip to content
Home » Programming » C# » Stack, Heap, Value Type & Reference Type in C#

Stack, Heap, Value Type & Reference Type in C#

Stack Heap Value And Reference Type in CSharp

Understanding Stack and Heap in C#

In this article, we’ll understand about the stack and heap memory in C#. In .NET application, there’re two types of memory allocation for the variable we create; i.e., stack memory and heap memory.

For the better understanding, let’s take an example,

using System;

namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            /*Statement 1*/
            int num1 = 111;

            /*Statement 2*/
            int num2 = 222;

            /*Statement 3*/
            Class1 cls1 = new Class1();
        }
    }
    class Class1
    {

    }
}

Statement 1

The compiler allocates some memory in the stack as soon as the first statement is executed. The stack memory keeps track of the running memory in your application. To better understand, please take a look at the following image.

stack-heap-value-type-reference-type

Statement 2

As soon as the second statement is executed, it stacks this memory allocation (memory allocation for variable num2) on top of the first memory allocation (memory allocation for variable num1). You can think about the stack as a series of chairs put on top of each other. To better understand, please take a look at the following image.

stack-heap-value-type-reference-type

Statement 3

In the third statement we created an object cls1 of Class1. When the third statement is executed, a pointer to the stack is created internally and the actual object is stored elsewhere, called the heap. Heap locations do not track running memory. The heap is used allocate dynamic memory. Refer the following diagram for better understanding.

stack-heap-value-type-reference-type

Value type and Reference type

Now that we understand the concepts of stack and heap, let’s dive deeper into the concept of value and reference types. A value type is a type that contains both data and storage in the same place. Reference types, on the other hand, are types that have pointers to actual memory locations.

Value Types in C#

In C#, a data type that directly holds a variable’s value in its own memory space is a value type, and a value type uses stack space to store the variable’s value.

Let us understand value types with an example. Please see the following photo. As you can see, we first create an integer variable called num1 and assign the integer value of num1 to another integer variable called num2. In this case, the memory allocation for these two variables is done on the stack.

value-type

Examples of Value Types

boolbytechardecimaldoubleenumfloatlong
sbyteintshortulongstructuintushort
Value Type Datatypes in C#

Reference Types in C#

In C#, a reference type contains a pointer to another memory location that contains data. Reference types do not store variable values ​​directly in memory. Instead, it stores the memory address of the variable value, indicating where the value is stored.

Let us understand reference types with an example. Please see the following photo. Here we first create an object (obj1) and then assign this object to another object (obj2). In this case both reference variables (obj1 and obj2) point to the same memory location.

reference-type
classinterfaceobjectstringdelegate
Reference Type Datatypes in C#

Stack, Heap, Boxing, Unboxing, Value Types and Reference Types in Depth by Questpond

Stack, Heap, Boxing, Unboxing, Value Types and Reference Types in Depth by Questpond

Also Read:
C# for Beginners.
Garbage Collection in C# .NET

Leave a Reply

Your email address will not be published. Required fields are marked *