Skip to content

1. VALUE TYPES vs REFERENCE TYPES

Definition

  • Value Types

  • Store actual data directly

  • Assignment creates a copy
  • Typically allocated on stack (but can exist on heap when inside objects)

  • Reference Types

  • Store reference (pointer) to object

  • Assignment copies the reference
  • Object lives on heap

Memory Model

Value Type:

Stack:
a = 10
b = 10 (copy)

---

Reference Type:

Stack:
p1 → 0x100
p2 → 0x100

Heap:
0x100 → { Age = 20 }

Example — Value Copy

int a = 10;
int b = a;

b = 20;

Console.WriteLine(a); // 10
Console.WriteLine(b); // 20

Example — Reference Copy

class Person { public int Age; }

var p1 = new Person { Age = 10 };
var p2 = p1;

p2.Age = 20;

Console.WriteLine(p1.Age); // 20

Example — Value inside Reference

struct Point { public int X; }

class Holder
{
    public Point P;
}

var h = new Holder();
h.P.X = 10;
Heap:
Holder:
    P.X = 10 (inline, no separate object)

Interview Answer

Value types store actual data and are copied on assignment, while reference types store references to heap objects and share the same underlying instance. This difference affects mutation behavior and memory allocation.



2. ref / out / in

Definition

  • ref: pass by reference (read + write)
  • out: pass by reference (must assign inside method)
  • in: pass by reference (read-only)

Example — ref

void Increment(ref int x)
{
    x++;
}

int a = 10;
Increment(ref a);

Console.WriteLine(a); // 11

Example — out

void Create(out int x)
{
    x = 50;
}

int a;
Create(out a);

Console.WriteLine(a); // 50

Example — in

void Print(in int x)
{
    Console.WriteLine(x);
}

Memory Model

Without ref:
    method gets copy

With ref/out/in:
    method works on same memory location

Interview Answer

The ref keyword allows passing variables by reference for both reading and writing. Out is used for output parameters and must be assigned inside the method, while in passes by reference but enforces read-only access. All three avoid copying large data.



3. BOXING / UNBOXING

Definition

  • Boxing: converting value type → object (heap allocation)
  • Unboxing: extracting value type from object

Example

int x = 10;
object obj = x; // boxing
Heap:
0x200 → boxed int (10)

Stack:
obj → 0x200

int y = (int)obj; // unboxing

Hidden Boxing

int x = 10;
IComparable c = x; // boxing

Performance Impact

  • Heap allocation
  • Copying value
  • GC pressure

Interview Answer

Boxing converts a value type into an object by allocating it on the heap, while unboxing retrieves the value. Boxing introduces performance overhead and should be avoided in performance-critical paths.



4. STRUCTS

Definition

  • Value type
  • Stored inline
  • Copied on assignment
  • No inheritance (except interfaces)

Example — Copy Behavior

struct Point
{
    public int X;
}

var p1 = new Point { X = 10 };
var p2 = p1;

p2.X = 20;

Console.WriteLine(p1.X); // 10

Example — Mutable Struct Problem

struct Counter
{
    public int Value;
    public void Increment() => Value++;
}

var list = new List<Counter>();
list.Add(new Counter());

list[0].Increment(); // operates on copy

Console.WriteLine(list[0].Value); // 0

Memory Behavior

Struct in List:

List → stores struct value
Access → returns copy

When to Use

  • Small data
  • Immutable
  • High-performance scenarios

Interview Answer

Structs are value types stored inline and copied on assignment. They are suitable for small, immutable data. Mutable structs can lead to bugs due to unintended copying, especially when used in collections.



5. RECORDS

Definition

  • Reference type
  • Value-based equality
  • Designed for immutability

Example — Basic

record Person(string Name);

var p1 = new Person("A");
var p2 = new Person("A");

Console.WriteLine(p1 == p2); // true

Example — with expression

var p2 = p1 with { Name = "B" };

Memory

p1 → 0x100 → { Name = "A" }
p2 → 0x200 → { Name = "B" }

Behavior

  • Equality based on values
  • Immutable by default
  • Generates:

  • Equals

  • GetHashCode
  • ToString

Interview Answer

Records are reference types that provide value-based equality and are designed for immutable data models. They simplify scenarios where objects represent data rather than identity and support non-destructive mutation using the with keyword.



6. CLASS vs STRUCT vs RECORD

Definition Comparison

Feature Class Struct Record
Type Reference Value Reference
Allocation Heap Stack/Inline Heap
Copy Reference copy Value copy Reference
Equality Reference Value (field-wise) Value-based
Mutability Mutable Usually mutable Immutable by default

Example Comparison

class A { public int X; }
struct B { public int X; }
record C(int X);

Behavior

var a1 = new A { X = 10 };
var a2 = a1;
a2.X = 20;
// a1.X = 20

var b1 = new B { X = 10 };
var b2 = b1;
b2.X = 20;
// b1.X = 10

var c1 = new C(10);
var c2 = new C(10);
// c1 == c2 → true

Interview Answer

Classes are reference types used for modeling identity and behavior, structs are value types optimized for small data and copied on assignment, and records are reference types designed for immutable data with value-based equality. The choice depends on whether identity, performance, or immutability is the primary concern.