Suppose I have an int
variable defined in a C# program. Regardless of the value stored, it is always going to take 32 bits of memory. How do nullable types behave in this regard? If an int?
variable is set to null
, will it still occupy 32 bits of space?
Asked
Active
Viewed 195 times
1

Connor Low
- 5,900
- 3
- 31
- 52

Aadith Ramia
- 10,005
- 19
- 67
- 86
-
Is you question about `int` or `Nullable
`? – Progman Jan 26 '22 at 18:59
1 Answers
3
If the reference source is anything to go by, Nullable<T>
always takes up the size of T
+ the size of a bool
indicating if T
is valid or not. So in your case it would take 4
+ 1
(bool
isn't bitpacked, so it takes a full byte) = 5
bytes. This doesn't include any runtime-specific padding or optimizations.

Joe Sewell
- 6,067
- 1
- 21
- 34