In C#, Value Types and Reference Types define how data is stored and passed in memory.
This is one of the most important C# interview questions
Quick Interview Answer
-
Value Type → Stores actual value
-
Reference Type → Stores reference (memory address)
1. Value Type
Value types store actual data directly in memory.
Example
int a = 10;
int b = a;
b = 20;
Console.WriteLine(a); // 10
Console.WriteLine(b); // 20
Why?
Because a and b are separate copies 📦
a = 10
b = 10 → changed to 20
They don't affect each other.
2. Reference Type
Reference types store memory address, not actual value.
Example
class Person
{
public string Name { get; set; }
}
Person p1 = new Person();
p1.Name = "John";
Person p2 = p1;
p2.Name = "David";
Console.WriteLine(p1.Name); // David
Why?
Because p1 and p2 point to same object 🎯
p1 ─┐
├──> Object in memory
p2 ─┘
So changing one affects both.
Memory Difference
| Value Type | Reference Type |
|---|---|
| Stored in Stack | Stored in Heap |
| Stores actual value | Stores reference |
| Faster | Slightly slower |
| Independent copy | Shared reference |
Examples
Value Types
int
double
bool
DateTime
struct
enum
Reference Types
class
string
object
array
interface
delegate
Struct vs Class Example
Struct → Value Type
struct Point
{
public int X;
public int Y;
}
Class → Reference Type
class Point
{
public int X;
public int Y;
}
Important Interview Question
What happens when passing to method?
Value Type
void Change(int x)
{
x = 100;
}
int a = 10;
Change(a);
Console.WriteLine(a); // 10
Does NOT change.
Reference Type
void Change(Person p)
{
p.Name = "Changed";
}
Changes original object.
Visual Diagram
Value Type
a = 10
b = 10 (copy)
Reference Type
p1 → Object
p2 → Same Object
Interview Table
| Feature | Value Type | Reference Type |
|---|---|---|
| Memory | Stack | Heap |
| Copy | Actual value | Reference |
| Performance | Faster | Slower |
| Default value | 0 | null |
| Examples | int, struct | class, string |
Interview One-Line Answer
Value types store actual data, while reference types store memory references to objects.
