Advertisement
Beginner C#, Value vs Reference Types

What is the difference between a value type and a reference type?

Short answer

Value types (struct, enum, primitives) hold their data directly and are copied on assignment; reference types (class, array, string, delegate) hold a reference to data on the heap, so assignment copies the reference.

Save C# mock interview
Advertisement
AModel answer
Value types (struct, enum, primitives) hold their data directly and are copied on assignment; reference types (class, array, string, delegate) hold a reference to data on the heap, so assignment copies the reference.
int a = 1; int b = a; b++;       // a stays 1
var l1 = new List<int>(); var l2 = l1; l2.Add(9); // l1 also has 9
Real-world example
Passing a struct point copies it; passing a class order shares the same instance.