What I was getting at is: there is no such thing as a boxed nullable. When you box an int
, you get a reference to a boxed int
. When you box an int?
, you get either a null reference or a reference to a boxed int
. You never get a boxed int?
.
You can easily make your own Optional<T>
struct, but you can't implement a struct that has that boxing behaviour. Nullable<T>
's special behaviour is baked into the runtime.
This fact leads to a number of oddities. For example:
And FYI there are other ways in which the Nullable<T>
type is "magical". For instance, though it is a struct type, it does not satisfy the struct constraint. There's no way for you to make your own struct that has that property.