Advertisement
Advanced C#, Value vs Reference Types

How do nullable value types (int?) work under the hood?

Short answer

int? is Nullable , a struct with HasValue and Value. The compiler lifts operators so null propagates; boxing a null Nullable boxes to a real null reference.

Save C# mock interview
Advertisement
AModel answer
int? is Nullable<int>, a struct with HasValue and Value. The compiler lifts operators so null propagates; boxing a null Nullable<T> boxes to a real null reference.
int? x = null;
object o = x;              // o is null
int safe = x.GetValueOrDefault();
Real-world example
Mapping a nullable database column to int? preserves 'no value' distinctly from 0.