可以在類型或成員的聲明中使用 unsafe 修飾符。因此,類型或成員的整個正文範圍均被視爲不安全上下文。例如,以下是用 unsafe 修飾符聲明的方法:
- unsafe static void FastCopy(byte[] src, byte[] dst, int count)
- {
- // Unsafe context: can use pointers here.
- }
不安全上下文的範圍從參數列表擴展到方法的結尾,因此指針在以下參數列表中也可以使用:
- unsafe static void FastCopy ( byte* ps, byte* pd, int count ) {...}
還可以使用不安全塊從而能夠使用該塊內的不安全代碼。例如:
- unsafe
- {
- // Unsafe context: can use pointers here.
- }
若要編譯不安全代碼,必須指定 /unsafe 編譯器選項。無法通過公共語言運行庫驗證不安全代碼。
- // cs_unsafe_keyword.cs
- // compile with: /unsafe
- using System;
- class UnsafeTest
- {
- // Unsafe method: takes pointer to int:
- unsafe static void SquarePtrParam(int* p)
- {
- *p *= *p;
- }
- unsafe static void Main()
- {
- int i = 5;
- // Unsafe method: uses address-of operator (&):
- SquarePtrParam(&i);
- Console.WriteLine(i);
- }
- }
輸出
25
fixed 語句禁止垃圾回收器重定位可移動的變量。fixed 語句只能出現在不安全的上下文中。Fixed 還可用於創建固定大小的緩衝區。
fixed 語句設置指向託管變量的指針並在 statement 執行期間“釘住”該變量。如果沒有 fixed 語句,則指向可移動託管變量的指針的作用很小,因爲垃圾回收可能不可預知地重定位變量。C# 編譯器只允許在 fixed 語句中分配指向託管變量的指針。
- // assume class Point { public int x, y; }
- // pt is a managed variable, subject to garbage collection.
- Point pt = new Point();
- // Using fixed allows the address of pt members to be
- // taken, and "pins" pt so it isn't relocated.
- fixed ( int* p = &pt.x )
- {
- *p = 1;
- }
可以用數組或字符串的地址初始化指針:
- fixed (int* p = arr) ... // equivalent to p = &arr[0]
- fixed (char* p = str) ... // equivalent to p = &str[0]
只要指針的類型相同,就可以初始化多個指針:
fixed (byte* ps = srcarray, pd = dstarray) {...}
要初始化不同類型的指針,只需嵌套 fixed 語句:
- <pre class="csharp" name="code">fixed (int* p1 = &p.x)
- {
- fixed (double* p2 = &array[5])
- {
- // Do something with p1 and p2.
- }
- }
注意 |
---|
無法修改在 fixed 語句中初始化的指針。 |
在不安全模式中,可以在堆棧上分配內存。堆棧不受垃圾回收的制約,因此不需要被鎖定。有關更多信息,請參見 stackalloc。
- // statements_fixed.cs
- // compile with: /unsafe
- using System;
- class Point
- {
- public int x, y;
- }
- class FixedTest
- {
- // Unsafe method: takes a pointer to an int.
- unsafe static void SquarePtrParam (int* p)
- {
- *p *= *p;
- }
- unsafe static void Main()
- {
- Point pt = new Point();
- pt.x = 5;
- pt.y = 6;
- // Pin pt in place:
- fixed (int* p = &pt.x)
- {
- SquarePtrParam (p);
- }
- // pt now unpinned
- Console.WriteLine ("{0} {1}", pt.x, pt.y);
- }
- }
輸出
25 6