当前位置:首页 > 代码相关 > 正文内容

C# 使用不安全的代码

admin6年前 (2020-03-20)代码相关3064

首先需要在 项目->属性->生成 中勾选允许不安全代码

下面的代码使用了指针,通过指针修改结构体的成员

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;
    }
}


扫描二维码推送至手机访问。

版权声明:本文由lovedm.club发布,如需转载请注明出处。

本文链接:https://lovedm.club/?id=18

分享给朋友:

“C# 使用不安全的代码” 的相关文章

九九乘法表算法

九九乘法表算法

namespace _20200324 {     class Program     {         st...

C#事件_Sample_2

事件的拥有者与事件的响应者是分开的情况+=是事件订阅操作符,左边是事件,右边是事件处理器。using System; using System.Windows.Forms; /// <summary> /// 事件的拥有者和事件的响应者是...

C# 泛型委托

C# 泛型委托

虽然没有必要,但是还是先看看自定义的泛型委托:namespace _20200402 {     class Program     {     &nb...

C# 正则表达式(1)

C# 正则表达式(1)

用于匹配输入文本的模式string s = "this is a test!"; string res = Regex.Replace(s, "^",&nbs...

C# 正则表达式(2)

// pattan = @"[^ahou]"; 表示匹配除ahou之外的字符,^在表示反义 string res4 = Regex.Replace(s, @"[^ahou]",&...

C# 与文件相关的几个类(1)

C# 与文件相关的几个类(1)

C# 与文件访问相关的常用的类:File类、Directory类、Path类、FileInfo类、DirectoryInfo类、FileSystemInfo类、FileSystemWatcher类以上几个类均在System.IO命名空间下。挨个说吧:File类:静态类,只有静态方法,用于移...