Skip to content

About

Hands-on C# types and memory — value vs reference, boxing measured, struct pitfalls, GC generations live, IDisposable. Part 06 of a practice series.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

Repository files navigation

C# Types & Memory — Hands-on Examples

Part 06 of a hands-on C# practice series. The topics that famously decide interviews: value vs reference types (with the nuance most people miss), boxing measured in megabytes, mutable-struct horror stories, GC generations watched live via GC.GetGeneration, and IDisposable/using ownership rules.

Running

dotnet run        # all examples
dotnet run 3      # just one (e.g. structs)

What's inside

# Topic Highlights
01 Value vs reference copy vs share, ref/out/in, "the reference is passed by value", where values really live
02 Boxing identity split, wrong-type unbox, 1M boxes measured in MB, sneaky boxing sites
03 Structs vs classes readonly record struct Money, the vanishing increment horror, ≤16-byte guidance
04 Garbage collector generations watched live, LOH, roots, why GC.Collect() is wrong
05 IDisposable three spellings of using, dispose-on-exception, IAsyncDisposable, ownership rules

The interview lines to remember

  • "The reference itself is passed by value" — explains every parameter puzzle.
  • "Value types live inline in their container — the container decides stack vs heap."
  • "Unbox must match the exact type; (long)boxedInt throws."
  • "Dispose is about non-memory resources and timing; the GC is about memory."
  • "Leaks in C# are accidental reachability: static lists, forgotten -=, unbounded caches."

Further reading

The series

← Previous: 05 Generics · Next: 07 Exceptions →

Part Topic
01 Collections
02 LINQ
03 Delegates & events
04 Async/await
05 Generics
06 Types & memory (this repo)
07 Exceptions
08 Modern C# 8–12
09 Strings & text
10 Files & JSON
11 MiniShop microservices (finale)

Author

Matej Zlatunić

License

Released under the MIT License. Feel free to use it for learning.

About

Hands-on C# types and memory — value vs reference, boxing measured, struct pitfalls, GC generations live, IDisposable. Part 06 of a practice series.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages