Size of custom class

  • Thread starter Thread starter PeterB
  • Start date Start date
P

PeterB

Is there a easy way to calculate the size of a custom class at a particular
moment in the code? I.e. I fill it up with standard data to get a average
size of the object...

regards,

Peter
 
Hi Peter,

To get the size of an object form its instance you can use the Marshal
class.

using System.Runtime.InteropServices;

class myClass
{
int x;
int y;
}
private void Form1_Load(object sender, System.EventArgs e)
{
myClass m1 = new myClass();
int mSize = Marshal.SizeOf(m1);
}
 
Keep in mind though that Marshal.SizeOf will not calculate
the size correctly if you'd have reference types or array
members.
 
Back
Top