.NET Internals7h estimated

Value Types vs. Reference Types

Difficulty
Importance

The problem

Assigning one variable to another, or passing a value into a method, means something different depending on whether that value is copied entirely or shared by reference — getting this wrong produces exactly the aliasing bugs (or unexpected performance costs from excessive copying) that come from assuming the wrong one.

Why now

Mutability and references already established the general copy-versus-share distinction; .NET makes that distinction unusually explicit and pervasive — every type in C# is declared as a struct (value type) or a class (reference type), a first-class language-level choice rather than an implicit rule based on the value's kind.

Mental model

A value type (struct, int, bool) is copied in full on assignment or when passed to a method — two variables holding a value type never share the same underlying data. A reference type (class) behaves like mutability-and-references already described for objects: the variable holds a pointer to shared data on the heap, so copying the variable copies the pointer, not the data. Choosing struct versus class in C# is choosing this behavior deliberately, not discovering it after the fact.

Requires

Used in

Projects

  • Write a struct and a class with identical fields, pass each into a method that modifies a field, and demonstrate that the struct's caller sees no change while the class's caller does
  • Benchmark allocating a large number of small structs versus small classes in a tight loop, and explain the performance difference in terms of stack versus heap allocation

Examples

  • Passing a struct into a method and modifying a field inside that method leaves the caller's original copy completely untouched — the method received a full copy, not a reference
  • Two variables both holding the same class instance and one setting a field via one variable is visible through the other, because both point to the same heap object — the identical aliasing behavior mutability-and-references already covers

Resources

Mastery checklist