0

How to get size of an object of a class/struct in C#.

hello h = new hello();
Base b = new Base();
Console.Write(Marshal.SizeOf(h) + "," + Marshal.SizeOf(b));
Console.Read();

I tried above code, but it gives error. ERROR:

Type 'Virtual.Base' cannot be marshaled as an unmanaged structure; no meaningful size or offset can be computed.

THanks!

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
CODError
  • 779
  • 2
  • 10
  • 25
  • Take at look at http://stackoverflow.com/questions/3361986/how-to-check-the-number-of-bytes-consumed-by-my-structure – dotNET Jan 11 '14 at 14:52
  • 2
    http://stackoverflow.com/questions/207592/getting-the-size-of-a-field-in-bytes-with-c-sharp#207605 – Sam Leach Jan 11 '14 at 14:52
  • 2
    In general the "size" of a class is an implementation detail of the framework used. For example they could make it depend on the CPU architecture. What do you need the size for? Do you wish a memory profile of your program? – Jeppe Stig Nielsen Jan 11 '14 at 14:55

0 Answers0