Size of a class

  • Thread starter Thread starter Jack
  • Start date Start date
Depending on what you want, specifically, there are a few answers.
For managed classes, there is no way to take the size of the class.
Conceptual class sizes are not set in stone in any way, a type can be of
many different sizes based on its contents. Considering both "One String"
and "A much longer string that has alot of extra words in it" are both of
type string, how do you determine size just by passing string? The header,
etc size should be determinable, but I don't know how much value that would
have for you, as you should rarely, if ever, be messing with such things.
You can, if you are really interested in the size of a given object graph at
a given point in time serialize it and determine the length of the output.
This may be transient however because other objects can be added or removed
at any time, or it may be downright incorrect because some values are tossed
out during serialization.
Value Types, structs in C#, that have no managed type members can have their
size retreived using sizeof(type) in unsafe C# code, and are the suggested
choice for interop, etc. Note however that arrays in structs are managed
objects. C# 2.0 is slated to introduce a feature that allows for fixed sized
inline valuetype arrays, but that is not here yet.
 
Back
Top