Explain and contrast value types and reference types in C#

In C#, all types can be divided into two main categories − value types and reference types. Understanding the difference between these types is crucial for memory management and avoiding common programming errors.

Value Types

Variables of value types directly contain their data. Each variable has its own copy of the data, stored on the stack. When you assign one value type variable to another, the entire value is copied.

Value types in C# include −

  • All numeric types: int, float, double, decimal, byte, etc.

  • char and bool types

  • struct types

  • enum types

Value Type Memory Layout Variable p1 X=10, Y=5 Variable p2 X=10, Y=5 p2 = p1 (copies the values) Each variable has its own independent copy Changing p2 does NOT affect p1

Creating Custom Value Types

You can create custom value types using struct

using System;

public struct Point {
   public int X;
   public int Y;
   
   public Point(int x, int y) {
      X = x;
      Y = y;
   }
}

class Program {
   public static void Main() {
      Point p1 = new Point(10, 5);
      Point p2 = p1;  // Creates a copy
      p2.X = 20;      // Only affects p2
      
      Console.WriteLine("p1: X=" + p1.X + ", Y=" + p1.Y);
      Console.WriteLine("p2: X=" + p2.X + ", Y=" + p2.Y);
   }
}

The output of the above code is −

p1: X=10, Y=5
p2: X=20, Y=5

Reference Types

Variables of reference types store a reference (memory address) to their objects, not the actual data. The actual object is stored on the heap. Multiple variables can reference the same object, and changes made through one variable are visible through all references.

Reference types in C# include −

  • string

  • class types

  • array types

  • delegate types

  • interface types

Reference Type Memory Layout Variable u1 Variable u2 User Object Age = 20 reference reference Both variables point to the same object Changes through u2 are visible in u1

Example with Reference Types

using System;

public class User {
   public int Age { get; set; }
   public string Name { get; set; }
}

class Program {
   public static void Main() {
      User u1 = new User { Age = 10, Name = "Alice" };
      User u2 = u1;  // Copies the reference, not the object
      u2.Age = 20;   // Modifies the shared object
      
      Console.WriteLine("u1 Age: " + u1.Age);  // Shows 20
      Console.WriteLine("u2 Age: " + u2.Age);  // Shows 20
      Console.WriteLine("Same object: " + (u1 == u2));  // True
   }
}

The output of the above code is −

u1 Age: 20
u2 Age: 20
Same object: True

Complete Comparison Example

using System;

public struct Point {  // Value type
   public int X;
   public int Y;
}

public class User {    // Reference type
   public int Age { get; set; }
}

class Program {
   public static void Main() {
      // Value type behavior
      var p1 = new Point { X = 10 };
      Point p2 = p1;
      p2.X = 20;
      Console.WriteLine("Value Type:");
      Console.WriteLine("p1.X = " + p1.X);  // 10
      Console.WriteLine("p2.X = " + p2.X);  // 20
      
      // Reference type behavior
      var u1 = new User { Age = 10 };
      User u2 = u1;
      u2.Age = 20;
      Console.WriteLine("\nReference Type:");
      Console.WriteLine("u1.Age = " + u1.Age);  // 20
      Console.WriteLine("u2.Age = " + u2.Age);  // 20
   }
}

The output of the above code is −

Value Type:
p1.X = 10
p2.X = 20

Reference Type:
u1.Age = 20
u2.Age = 20

Key Differences

Value Types Reference Types
Store data directly in the variable Store a reference to the data
Stored on the stack Stored on the heap
Assignment copies the value Assignment copies the reference
Each variable is independent Multiple variables can reference the same object
Cannot be null Can be null

Conclusion

Value types store data directly and provide independent copies when assigned, while reference types store memory addresses and allow multiple variables to share the same object. Understanding this distinction is essential for proper memory management and avoiding unintended side effects in C# programming.

Updated on: 2026-03-17T07:04:36+05:30

407 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements