来了很久一直没注册今天第一天哈哈写个帖帖练习。
由于c#弱化了对堆空间的内存管理,不需要开发者关注。
我们想知道new 的类到底占多少空间怎么办呢?
举例:
[StructLayout(LayoutKind.Sequential)] //必须加上特性
public class Person
{
private int a;
public Person()
{
}
}
using System.Runtime.InteropServices;
namespace ConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
Person person = new Person();
Console.WriteLine(Marshal.SizeOf(person)); //输出了 4 也就是person 类占了4个字节的空间
Console.Read();
}
}
}