class1.cs

来自「C#开发教程 由浅入深 配有实例 是初学者的好帮手」· CS 代码 · 共 40 行

CS
40
字号
using System;

namespace UnsafeClass
{
    public class Insect
    {
        public int id;
        public Insect(int id) { this.id = id; }
    }

    class UnsafeClassApp
    {
        unsafe public static void Swap(int* pi, int* pj)
        {
            int tmp = *pi;
            *pi = *pj;
            *pj = tmp;
        }
        [STAThread]
        static void Main(string[] args)
        {
            Insect i = new Insect(123);
            Insect j = new Insect(456);
            Console.WriteLine(
                "Before Swap:\ti = {0}, j = {1}", i.id, j.id);

            unsafe
            {
                fixed (int* pi = &i.id, pj = &j.id)
                {
                    Swap(pi, pj);
                }
            }

            Console.WriteLine(
                "After Swap:\ti = {0}, j = {1}", i.id, j.id);
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?