In our own Jon Skeet’s C# in depth, he discusses the 3 ways to simulate a ‘null’ for value types:
- Magic value (e.g. earliest possible DateTime is taken to be ‘null’)
- Reference type wrapper
- boolean flag
It is mentioned that nullable types use the third method. How exactly do nullable types work under the hood?
Ultimately, they are just a generic struct with a bool flag – except with special boxing rules. Because structs are (by default) initialized to zero, the bool defaults to false (no value):
Extra differences, though:
EqualityComparer<T>,Comparer<T>etc)Nullable<Nullable<T>>)