How much memory in bytes do types like int
, bool
, float
, double
, decimal
, object
, and string
use when added as a field to an instance of a class?
Floating-point numbers use the IEEE (Institute of Electrical and Electronics Engineers) format. Single-precision values with float type have 4 bytes, consisting of a sign bit, an 8-bit excess-127 binary exponent, and a 23-bit mantissa.
The fact that an int uses a fixed number of bytes (such as 4) is a compiler/CPU efficiency and limitation, designed to make common integer operations fast and efficient.
This page shows the size of each datatype in bits (divide by 8 to get bytes):
byte 8 sbyte 8 int 32 uint 32 short 16 ushort 16 etc...
Object and string are reference types. Reference types take up at least the size of all the fields they contain plus the size of the reference itself.
Related
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With