Slider

Value Types vs Reference Types in C#

Learn the difference between value types and reference types in C#, including memory behavior, assignment, and performance implications.

Understanding the difference between Value Types and Reference Types is one of the most important fundamentals in C#. It directly impacts memory usage, performance, method behavior, and bug prevention.

In this article, we will explore what value types and reference types are, how they behave in memory, how assignment and method calls differ between them, and when to use each one in real applications.

What is Value Type in C#?

Value types store actual data directly. When a value type variable is assigned to another variable, a copy of the data is created. Each variable maintains its own independent copy.

Common value types include:

  • int
  • float, double, decimal
  • bool
  • struct
  • enum
Example:
int a = 10;
int b = a;

b = 20;

Console.WriteLine(a); // 10
Console.WriteLine(b); // 20
Here, changing b does not affect a because b received a copy of a.

What is Reference Type in C#?

Reference types store a reference (address) to the actual data rather than the data itself. When a reference type variable is assigned to another variable, both variables point to the same object in memory.

Common reference types include:
  • class
  • string
  • array
  • object
  • interface
  • delegate
Example:
class Person
{
    public int Age;
}

Person p1 = new Person { Age = 25 };
Person p2 = p1;

p2.Age = 30;

Console.WriteLine(p1.Age); // 30
Both p1 and p2 refer to the same object, so changes made through one reference affect the other.

Pass By Value Vs Pass By Reference.

In C#, how data is passed to a method determines whether the original value can be changed. This is commonly explained using pass by value and pass by reference.

1. Pass By Value: Pass by value means a copy of the variable is passed to the method. Any change made inside the method does not affect the original variable. By default, all method parameters in C# are passed by value.

Example
void ChangeValue(int number)
{
    number = 50;
}

int x = 10;
ChangeValue(x);

Console.WriteLine(x); // Output: 10
Here, number receives a copy of x. Modifying the number does not change x.

2. Pass By Reference: Pass by reference means the method receives a reference to the original variable itself, allowing the method to modify the original variable. In C#, this is done using the ref or out keywords.

Example Using ref:
void ChangeValue(ref int number)
{
    number = 50;
}

int x = 10;
ChangeValue(ref x);

Console.WriteLine(x); // Output: 50
Here, the method modifies the original variable directly.

Example Using out:
void GetValues(out int result)
{
    result = 100;
}

int x;
GetValues(out x);

Console.WriteLine(x); // Output: 100
out is used when the method is expected to assign a value.

How Memory Allocation Works in C#?

In C#, memory is mainly managed using two logical areas: the stack and the heap. Understanding how they work helps explain performance, object lifetime, and behavior of value and reference types.

Stack Memory

The stack stores value types and method call information. Memory allocation on the stack is very fast and is automatically cleaned up when a method finishes execution.

Key points:

  • Stores local value-type variables
  • Follows Last-In-First-Out (LIFO)
  • Memory is freed automatically when the scope ends
Example:
void Test()
{
    int x = 10;   // stored on stack
}
When Test() ends, x is automatically removed from the stack.

Heap Memory

The heap stores reference-type objects. Memory allocation on the heap is slower, and cleanup is handled by the Garbage Collector.

Key points:
  • Stores objects created using new
  • Accessed via references
  • Cleaned by GC, not immediately
Example:
class Person
{
    public int Age;
}

void Test()
{
    Person p = new Person(); // object on heap, reference on stack
}
Here, p is on the stack, but the actual Person object is on the heap.

Why is string Immutable but still a Reference Type in C#?

At first glance, strings look confusing because they are reference types, yet they behave like value types when modified. This design is intentional, providing safety, performance, and reliability.
Note: An immutable object is one whose state cannot be changed after creation. Any modification creates a new object instead of changing the existing one.
A string is a reference type because:
  • Strings can be large and variable in size
  • Storing them on the heap avoids costly copying
  • Multiple variables can reference the same string instance
string s1 = "Hello";
string s2 = s1;
Here, both s1 and s2 initially point to the same string object in memory.

Immutability means once a string is created, its value cannot be changed. Any operation that appears to modify a string actually creates a new string object.
string s1 = "Hello";
string s2 = s1;

s2 = "World";

Console.WriteLine(s1); // Hello
Console.WriteLine(s2); // World
The original "Hello" string is untouched; a new "World" string is created.
String is a reference type for performance reasons, but immutable to ensure security, thread safety, and safe sharing through interning.

Value types store data directly and create copies during assignment, while reference types store references to objects in memory and share the same instance across assignments. Choosing between them correctly leads to better performance, safer code, and fewer bugs.
0

No comments

Post a Comment

both, mystorymag

DON'T MISS

Tech News
© all rights reserved
made with by AlgoLesson
Table of Contents