c# sizeof() 计算结构体所占的字节 And 自定义结构体
程序员文章站
2022-06-03 14:59:31
...
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
int x = sizeof(int);
// 4
Console.WriteLine(x);
int x7 = sizeof(uint);
// 4
Console.WriteLine(x7);
int x2 = sizeof(double);
// 8
Console.WriteLine(x2);
// 2
int x3 = sizeof(char);
Console.WriteLine(x3);
// 1
int x4 = sizeof(byte);
Console.WriteLine(x4);
int x5 = sizeof(decimal);
// 16
Console.WriteLine(x5);
// 获取自定义结构体的内存大小时
// 要放在unsafe上下文内
unsafe
{
int x6 = sizeof(Student);
// 16
Console.WriteLine(x6);
}
}
}
struct Student
{
int ID;
long Score;
}
}