或是:
class Test
{
static void Main() {
Point point;
unsafe {
Point* p = &point;
(*p).x = 10;
(*p).y = 20;
Console.WriteLine((*p).ToString());
}
}
}
指針元素訪問
在指針元素訪問P[E]中,P必須是除了void*之外的類型,同時E必須能隱式的轉換為int, uint, long, or ulong的表達式。
P[E]效果上同*(P + E).例如:
1 class Test
2 {
3 static void Main() {
4 unsafe {
5 char* p = stackalloc char[256];
6 for (int i = 0; i < 256; i++) p[i] = (char)i;
7 }
8 }
9 }
10 又如:
11 class Test
12 {
13 static void Main() {
14 unsafe {
15 char* p = stackalloc char[256];
16 for (int i = 0; i < 256; i++) *(p + i) = (char)i;
17 }
18 }
19 }
20