C#(3)操作内存时需要允许unsafe

39 阅读1分钟

Visual Studio 2022中

  1. 项目 - (项目名)属性

image.png

  1. 生成(build) - 允许不安全代码

image.png

    //结构体
    struct Student {
        public int ID;
        public long Score;
    }
    
    unsafe
            {

                Student stu;
                stu.ID = 11;
                stu.Score = 99;
                Student* pStu = &stu;
                pStu->Score = 100;
                Console.WriteLine(stu.Score);
            }