⚙️ C# / .NET
Beginner
What is the difference between value types and reference types in C#?
Answer
In C#, types are classified based on how they store and pass data. Value types store data directly on the stack (or inline in structures). Assigning a value type creates a copy — modifications don't affect the original. Examples: int, double, bool, char, struct, enum. Reference types store a reference (pointer) to data on the heap. Assigning copies the reference — both variables point to the same object; modifying one affects the other. Examples: class, string, array, interface, delegate. Boxing: converting a value type to object (reference type) — the value is copied to the heap. Unboxing: casting the object back to the value type. Boxing/unboxing is expensive and should be avoided in performance-critical code.