What is the reason not all value types are nullable?

.net, nullable

Solution

Various reasons:

- History: `Nullable<T>` didn't exist until .NET 2.0, and it can't break existing code - especially with the different boxing rules for `Nullable<T>`

- Meaning: if I want an `int`, I might not want it to be nullable... I want it to be an `int`, and I don't want to have to check-for/handle nulls

- Space: it adds extra cost to each struct... in particular, imagine a `byte[]`, and now consider if `byte` was nullable - lots of extra overhead; *plus it would stop you doing blits etc*

Performance: `Nullable<T>` adds lots of extra penalties; in particular lots of hidden calls to `.HasValue` and `.Value` / `.GetValueOrDefault()`; this is shown in particular in the "lifted operators" - i.e. `x + y` becomes something like below, which adds up for tight loops etc:

`(x.HasValue && y.HasValue) ? (x.GetValueOrDefault() + y.GetValueOrDefault()) : null`

Likewise, `x == y` has to check:

- if both null => true

- if one null => false

- otherwise use `==` on `GetValueOrDefault()` of each

lots of overhead....

Problem

Is there any penalty, such that you should only set them as nullable when you really need it? Thanks

Original source