C# 使用不安全的代码
首先需要在 项目->属性->生成 中勾选允许不安全代码
下面的代码使用了指针,通过指针修改结构体的成员
namespace _20200320
{
class Program
{
static void Main(string[] args)
{
unsafe
{
Student stu;
stu.ID = 1;
stu.Score = 100;
Student* pstu = &stu;
pstu->Score = 200;
Console.WriteLine(stu.Score);
(*pstu).Score = 1000;
Console.WriteLine(stu.Score);
}
Console.ReadKey();
}
}
struct Student
{
public int ID;
public int Score;
}
}
鲁公网安备 37148202000241号