Value Type and Reference Type in C#

In this article, we’ll look at the difference between a value type and a reference type in the C# programming language.

Pictorial representation of value type and reference type in C#

Data types in C# are illustrated in the diagram below.

Value type and reference type in C#
Data types in C#

The two main categories of C# types are as follows.

    • Value Type
    • Reference Type

Value Type In C#

Value types in C# are generally stored in the stack memory, while reference types are kept in the managed heap. A value type is a type that inherits from System.ValueType and stores data in its own memory allocation.

To put it another way, each variable, object, and value type has its own copy of the data.
In C#, a value type does not carry a reference; instead, it stores the actual value in its own stack memory location.
This means that these data types’ variables directly contain values.

There are two types of value types in C#:

    • Built-in value types in C#
    • User-defined value type in C#

int, long, float, double, byte, decimal, char, bool, short are a few examples of Built-in value types.

struct and enum are examples of user-defined value types.

Example-Value type in C#

Consider the code snippet below as an example of a value type in C#.

using System;
class Program
{
    static void Main()
    { 
        // Here, integer x and y variables are value types
        int x = 100;
        int y  =  x;
        // increment y by 1
            y++; 
      
        Console.WriteLine($"x = {x} y = {y}");
        Console.ReadLine();
    }
    // Output: x = 100 y = 101
}

Let’s execute the above program and see the result.

Value type output
The output of the above program.

Both x and y will be present on the stack memory, but they are separate entities.

Reference type in C#

All the reference types in C# are derived from System.Object class.
Instead of storing its value directly, a reference type carries a pointer to a memory address in the managed heap that holds the actual data.
For the reference type, the real object’s reference is utilized to store in the stack memory, while the object itself is stored on the heap memory.

Assigning a reference type variable to another does not transfer or copy the data since reference types represent the variable’s address rather than the data itself. Instead, it duplicates the reference, pointing to the original item’s heap address.

In the case of reference type, two variables can reference the same object in the heap memory. As a result, operations on one variable may have an impact on the object that the other variable refers to.

There are two types of reference types in C#

    • Built-in reference types
    • User-defined reference types

Built in reference type in C#:   object ,  string ,  dynamic 

User defined reference type in C#:  Class ,  Interface ,  Array ,  delegate 

Example-Reference type in C#

Let’s understand the reference type by using the following C# program.

using System;
namespace ReferenceType
{
    // Here, the 'User 'class is a reference type

    class User
    {
        // public field
        public int age = 10;
    }
    class Program
    {

        static void Main(string[] args)
        {
          // Creating object of the 'User' class.
            User user1 = new User();

          // Assign object user1 to the new object user2
            User user2 = user1;

          // Print the value of object user1 and user2
            Console.WriteLine($"user1.age = {user1.age} user2.age ={user2.age}");

          //Changed value of user2 object
            user2.age = 20;

          // Print the value of object user1 and user2
            Console.WriteLine($"user1.age = {user1.age} user2.age ={user2.age}");
        }
    }
}

Let’s execute the above program and see the result.

Reference type output
The output of the above program.

We can see in the above result that changing the value of User2 by 20 also changes the value of User1.
Both objects, of course, point to the same memory region in the managed heap.

Value Type Vs Reference Type in C#

      • The value types in C# are stored on the stack memory during compilation, whereas reference types are stored on the managed heap at the run time.
      • The value type variable directly holds the actual value, whereas the reference type variable contains the pointer or reference to a memory location that holds the actual data in the managed heap.
      • Value types in C# are derived from System.ValueType whereas Reference types in C# are derived from System.Object
      • When the value types go out of scope, they are automatically released from the stack memory, whereas the reference types require the use of a garbage collector to be removed from the managed heap.
      • In C#, value types cannot inherit from another class or struct except the interface, whereas the reference types can be inherited from another class or interface.

Understanding Value type in C#

In C#, a value type does not carry a reference; instead, it stores the actual value in its own stack memory.
For example, If we assign a value to the integer variable int x = 100, the value 100 will be stored in the same memory area as variable x.

Value type memory allocation
Memory allocation of a value type in stack memory.

Value types variables in C# have their own copy of the data. As a result, when we alter the value of one variable, the original variable is unaffected (except in the case of in, ref and out parameter )

Let’s create the second int variable, y, and assign the first variable’s value to it. The value will be copied from variable ‘x‘ to variable ‘y‘.
CLR (Common Language Runtime) will now generate two different memory locations for x and y in the stack, each of which is fully independent of the other and holds the same value of 100.

Value types assigning values.

Let’s now increase the value of the variable ‘y‘ by one. Because both variables have their own memory allocation, the value of the variable ‘x‘ will not change and will remain the same.

As a result, changes in one variable will have no impact on the other. In C#, these data types are referred to as Value Types.

Consider the following as an example.

Value types memory allocation
Value types memory allocation

We can see that the value of x remains the same and didn’t change after y is incremented by one in the above result
Because the variables ‘x‘ and ‘y‘ are in separate memory regions in the stack and have nothing to do with one another.

Is it true that value types in C# are always saved in stack memory?

It’s a common misperception that value types are always allocated on the stack memory; however, this is not the case. If the variable is a temporary or local variable, or if it is used directly inside a method, the value type will only be allocated on the stack.
If we declare a value type variable at the class level or within another reference type, however, it will be allocated in the GC heap memory.

When a value type variable is no longer in scope, it is automatically freed from memory. The Garbage Collector has no function in releasing the memory.

Note: There is an exceptional case with the String type. Even though the String is a reference type, because it is immutable, it behaves like a value type. When we concatenate or update the value, a new string object with the modified value is created in the managed heap.

To learn more about the string type you can read my previous post Difference between String and StringBuilder in C#

Reference MSDN: Value types, Reference types

Conclusion

In C#, there are two types: reference types and value types. Reference type variables keep pointers to their data (objects), whereas value type variables directly contain their data. Since two variables might refer to the same object using reference types, operations on one variable can have an impact on the object referenced by the other variable. Each variable in a value type has its own copy of the data, and operations on one variable cannot change the other.

Recommended Articles

    1. C# Struct vs Class |Top 15 Most Astounding Differences Between C# Struct and Class
    2. C# List Class |Top 12 Amazing Generic List Methods
    3. Generic Delegates in C# With Examples
    4. IEnumerable Interface in C# with examples
    5. Constructors in C# with Examples
    6. C# Enum | How to Use enumeration type in C#?
    7. Properties In C# with examples
    8. 10 Difference between interface and abstract class In C#
    9. What is a delegate in C#? Multicast Delegate with Examples

I hope you enjoyed this post; if you have any questions, please leave them in the comments box below.

Thanks for visiting. 🙂

One Reply to “Value Type and Reference Type in C#”

Leave a Reply