You can't in practical terms.
The class itself has a size, but because it contains a reference to another class (in this case a
string
) the size of the class is meaningless, because it will not incude the size of any possible references. While you could find out the size of the instance, that still is meaningless:
[StructLayout(LayoutKind.Sequential)]
class A { public string s; int i;}
[StructLayout(LayoutKind.Sequential)]
class B { int i;}
private void button2_Click(object sender, EventArgs e)
{
A a = new A();
a.s = "Hello there!";
B b = new B();
Console.WriteLine("A: {0}\nB: {1}", Marshal.SizeOf(a), Marshal.SizeOf(b));
}
Will give you:
A: 8
B: 4
which is not a lot of use to anyone!