If I have a class with 100 properties that are all int32’s, and I have instanced 100 of these objects then does it necessarily take up 40000 bytes (plus whatever other overhead an object takes) even before any of the properties are set or does some (or all) of that space remain unallocated until you actually assign a value to the preoperties for the first time?
Share
All class-scoped fields are “assigned” following instantiation, unlike locally-scoped variables, which can remain unassigned indefinitely. Thus, value types consume their appropriate size, and references consume the size of a moving pointer, no matter what–when they are scoped at the class level.
Note also that unless the layout is sequential (as in a struct) or explicit, most value types will be padded to at least 32 bits.
It’s not always straightforward to predict how much space a null reference will consume, but were they normal pointers, they would consume 4 bytes on x86 platforms and 8 bytes on x64 platforms.