public class A
{
int x;
float y;
}
How to find the size of the class in C#. Is there any operator like Sizeof(), which used to be in C++
public class A
{
int x;
float y;
}
How to find the size of the class in C#. Is there any operator like Sizeof(), which used to be in C++
If you use the
sizeof
operator, you'll get the size of the pointer (which will be the same for all objects, obviously).You could write your own method and iterate (using reflection) over all the object's members (or the ones you're interested in) and add the size of each individual member.
The tricky part would be able to determine the size of member that are not native types... your method would have to be somewhat "recursive".
If you were to implement the above idea, you'd have the approximate size of the object.
Most of the posted solutions are great and work fine but they have the restriction that your class must be serializable (for more details see: https://docs.microsoft.com/en-us/dotnet/standard/serialization/basic-serialization) However if you class is not serializable then the following code can be helpful. This code takes into consideration the fact that using a reference field is not free it adds some overhead. It also considers using an average size for strings so you get a better approximation.
Short answer:
You dont.
Long answer:
You can only do that if you type has a fixed layout and has no managed members. Structs are fixed by default. Classes can attributed to have a fixed layout.
(I am not showing how, as you really do not need it. It is only important when doing interop.)
You could serialize the class into a memory stream and then get the size from there, but I wouldn't really recommend doing this unless you had to.
The following would give you the size of the C# class:
Remember size of a class depends on padding.