How to determine if a Class in .NET is big or small? Is it measured on how many it\'s attributes or fields, datatype of its attributes/fields? or return type of methods? par
The size of a class instance is determined by:
So, typically a class containing a string property needs (on a 32 bit system):
And typically a class containing an integer property needs:
As you see, the string and integer properties take up the same space in the class, so in your first example they will use the same amount of memory.
The value of the string property is of course a different matter, as it might point to a string object on the heap, but that is a separate object and not part of the class pointing to it.
For more complicated classes, padding comes into play. A class containing a boolean and a string property would for example use:
Note that these are examples of memory layouts for classes. The exact layout varies depending on the version of the framework, the implementation of the CLR, and whether it's a 32-bit or 64-bit application. As a program can be run on either a 32-bit or 64-bit system, the memory layout is not even known to the compiler, it's decided when the code is JIT:ed before execution.