Roadmap › C# Language — Basics to Advanced › Module: Getting Started
Lessons in this course (38)

4. Value types vs reference types

Basic★ Must knowLesson 4 of 38
Value typesReference types
Examplesint, double, bool, char, decimal, struct, enum, DateTimeclass, string, arrays, delegate, interface, record (class)
Variable holdsThe data itselfA reference (address) to an object on the heap
AssignmentCopies the valueCopies the reference — both point to the same object
Default0, false, ...null
StoredInline: on the stack for locals, inside the containing object for fieldsObject on the managed heap
csharp
struct Point { public int X; }
class Box { public int X; }

var p1 = new Point { X = 1 };
var p2 = p1;      // copy
p2.X = 99;
Console.WriteLine(p1.X); // 1

var b1 = new Box { X = 1 };
var b2 = b1;      // same object
b2.X = 99;
Console.WriteLine(b1.X); // 99

Interview tip: "Value types live on the stack" is only partly true. A value-type field inside a class lives on the heap with that object. Say: value types are stored inline where they are declared.