ใน C# ชื่อ Array และ Pointer ที่ชี้ไปยัง Data Type เดียวกับ Array Data ไม่ใช่ Variable Type เดียวกัน เช่น int* p และ int[] p ไม่ใช่ Type เดียวกัน คุณสามารถ Increment Pointer Variable p ได้เพราะมันไม่ Fixed ใน Memory แต่ Array Address Fixed ใน Memory จึง Increment ไม่ได้
ตัวอย่าง
using System;
namespace UnsafeCodeApplication {
class TestPointer {
public unsafe static void Main() {
int[] list = {5, 25};
fixed(int* ptr = list)
/* let us have array address in pointer */
for (int i = 0; i < 2; i++) {
Console.WriteLine("Address of list[{0}] = {1}", i, (int)(ptr + i));
Console.WriteLine("Value of list[{0}] = {1}", i, *(ptr + i));
}
Console.ReadKey();
}
}
}
ผลลัพธ์
Address of list[0] = 31627168 Value of list[0] = 5 Address of list[1] = 31627172 Value of list[1] = 25