public class A
{
int x;
float y;
}
Run Code Online (Sandbox Code Playgroud)
如何在C#中查找类的大小.有没有像Sizeof()这样的运算符,它曾经是C++
小智 25
以下内容将为您提供C#类的大小:
Marshal.SizeOf(typeof(Myclass));
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential)]
class Myclass
{
}
Run Code Online (Sandbox Code Playgroud)
记住一个类的大小取决于填充.
简短回答:
你不.
答案很长:
如果您键入的内容具有固定布局且没有托管成员,则只能执行此操作.结构默认是固定的.类可以归因于具有固定布局.
(我没有展示如何,因为你真的不需要它.只有在互操作时才这么重要.)