码迷,mamicode.com
首页 > Windows程序 > 详细

C# 不安全代码

时间:2015-05-06 17:49:36      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:

当一个代码块使用unsafe修饰符标记时,C#允许在函数中使用指针变量。

不安全代码或非托管代码是指使用了指针变量的代码块。

下面的实例说明了 C# 中使用了 unsafe 修饰符时指针的使用:

using System;
namespace UnsafeCodeApplication
{
    class Program
    {
        static unsafe void Main(string[] args)
        {
            int var = 20;
            int* p = &var;
            Console.WriteLine("Data is: {0} ",  var);
            Console.WriteLine("Address is: {0}",  (int)p);
            Console.ReadKey();
        }
    }
}

为了编译不安全代码,您必须切换到命令行编译器指定 /unsafe 命令行。

例如,为了编译包含不安全代码的名为 prog1.cs 的程序,需在命令行中输入命令:

csc /unsafe prog1.cs

如果您使用的是 Visual Studio IDE,那么您需要在项目属性中启用不安全代码。

步骤如下:

  • 通过双击资源管理器(Solution Explorer)中的属性(properties)节点,打开项目属性(project properties)
  • 点击 Build 标签页。
  • 选择选项"Allow unsafe code"。


可以参考http://www.w3cschool.cc/csharp/csharp-unsafe-codes.html




C# 不安全代码

标签:

原文地址:http://blog.csdn.net/ilipan/article/details/45537613

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!