I'm looking for a way to get the size of an instance of a reference type. sizeof is only for value types. Is this possible?
Separation size, designated size, or control size used to define analyses of the products of a sizing operation.
It depends on OS version in windows. The size of the reference itself will depend on your processor architecture - 4 bytes on 32-bit, 8 bytes on 64-bit. The reference itself is basically a pointer. 32 bits on a 32 bit OS, 64 bits on a 64 bit OS.
sizeof() Operator in C# The sizeof() operator is used to obtain the size of a data type in bytes in bytes. It will not return the size of the variables or instances. Its return type is always int.
why reference size is always 4 bytes - c++ - Stack Overflow.
You need Marshal.SizeOf
Edit: This is for unsafe code, but then, so is sizeof().
If you don't mind it being a little less accurate than perfect, and for comparative purposes, you could serialize the object/s and measure that (in bytes for example)
EDIT (I kept thinking after posting): Because it's a little more complicated than sizeof for valuetypes, for example: reference types can have references to other objects and so on... there's not an exact and easy way to do it that I know of...
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