Unsafe code may only appear if compiling with /unsafe

要在vs.net中使用unsafe code 我们必须在项目的属性中设置一下,设置方法如下:

点项目属性(Properties)->生成(Build)->常规(General)中:钩上允许不安全代码(Allow unsafe code)

 

 

        public static void Main(string[] args)
        {
            int i = 99, y = 200;
            unsafe
            {
                swap(&i, &y);
            }
            Console.WriteLine("x is now {0},y is now {1}",i,y);
        }

        public static unsafe void swap(int *a, int *b)
        {
            int temp;
            temp = *a;
            *a = *b;
            *b = temp;
        }

 

你可能感兴趣的:(.net)