C# 不安全的程式碼,

來源:互聯網
上載者:User

C# 不安全的程式碼,

當一個代碼塊使用unsafe修飾符標記時,C#允許在函數中使用指標變數。

不安全的程式碼或Unmanaged 程式碼是指使用了指標變數的代碼塊。

下面的執行個體說明了 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




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.