C#進階編程七十五天----C#使用指標

來源:互聯網
上載者:User

標籤:運行   mil   eric   context   public   void   key   rgs   count   

在C#中使用指標的文法

假設想在C#中使用指標,首先對項目進行過配置:

 

 

 

 

 

 

看到屬性了嗎?單擊:

 

 

 

看到那個同意不安全的程式碼了嗎?選中

 

 

然後將有關指標,地址的操作放在unsafe語句塊中.使用unsafekeyword是告訴編譯器這裡的代碼是不安全的.

 

unsafekeyword的使用:

(1)放在函數前,修飾函數,說明在函數內部或函數的形參涉及到指標操作:

unsafe static void FastCopy(byte[] src, byte[] dst, int count)

{

    // Unsafe context: can use pointers here.

}

 

不安全內容相關的方位從參數列表擴充到方法的結尾,因此指標作為函數的參數時需使用unsafekeyword.

 

(2)將有關指標的操作放在由unsafe聲明的不安全塊中

unsafe{

//unsafe context:can use pointers here

}

 

 

案例:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace Csharp中使用指標

{

    class Program

    {

        static void Main(string[] args)

        {

            int i = 1;

            unsafe

            {

                Increment(&i);

            }

            Console.WriteLine(i+"\n");

 

            //示範怎樣使用指標操作字串

 

            string s = "Code Project is cool";

            Console.WriteLine("the original string : ");

            Console.WriteLine("{0}\n",s);

 

            char[] b = new char[100];

 

            //將指定書目的字元從此執行個體中的指定位置拷貝到Unicode字元數組的指定位置

            s.CopyTo(0, b, 0, 20);

 

 

 

            Console.WriteLine("the encode string : ");

 

            unsafe

            {

                fixed (char* p = b)

                {

                    NEncodeDecode(p); 

                }

            }

            for (int t = 0; t < 10; t++)

            {

                Console.WriteLine(b[t]);

            }

            Console.WriteLine("\n");

 

            Console.WriteLine("the decoded string : ");

 

            unsafe

            {

                fixed (char* p = b)

                {

                    NEncodeDecode(p); 

                }

            }

            for (int t = 0; t < 20; t++)

                Console.Write(b[t]);

            Console.WriteLine();

            Console.ReadKey();

        }

        unsafe public static void Increment(int* p)

        {

            *p = *p + 1;

        }

        /// <summary>

        /// 將一段字串通過異或運算進行編碼解碼的操作.假設您將一段字串送入這個

        /// 函數,這個字串會被編碼,假設將這一段已經編碼的字元送入這個函數,

        /// 這段字串就會

        /// 解碼

        /// </summary>

        /// <param name="s"></param>

        unsafe public static void NEncodeDecode(char* s)

        {

            int w;

            for (int i = 0; i < 20; i++)

            {

                w = (int)*(s + i);

                w = w ^ 5;//按位異或

                *(s + i) = (char)w;

            }

        }

 

    }

}

 

 

 

當中用到了fixed,以下對其進行必要的介紹:

fixed語句僅僅能出如今不安全的上下文中,C#編譯器僅僅同意fixed語句中分配指向託管變數的指標,無法改動在fixed語句中初始化的指標.

fixed語句禁止記憶體回收行程重定位可移動的變數.當你在語句或函數之前使用fixed時,你是告訴.net平台的記憶體回收行程,在這個語句或函數運行完成前,不得回收其所佔用的記憶體空間.

 

 

C#進階編程七十五天----C#使用指標

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.