How to find the size of a class in C#

前端 未结 5 624
一个人的身影
一个人的身影 2020-12-18 20:10
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++

5条回答
  •  感动是毒
    2020-12-18 20:49

    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.)

提交回复
热议问题