I’ve been conducting a lot of interviews lately for work. One of the questions that has grown over on me over time is to ask a C# developer “explain the difference between a value type and a reference type”. I used to not think very highly of this question since virtually all my C# coding uses reference types exclusively. Over time I’ve come to see that developers that can answer this question well, can do so because they have looked into how the nitty gritty of C# works. This inquisitive nature is exactly what I want in developers on my teams.
Without further preamble,
What is the difference between a value type and reference type in C#?
Basics: value types are structs and reference types are classes.
Intermediate: value types are allocated on the stack and reference types are allocated on the heap.
Advanced: value types are allocated on the stack when they’re locally instantiated. Static value types and value types which are member fields of classes are both allocated on the heap!
That’s it! And that concludes my first post!