C# Pointer Assignment Guide

In C#, the keyword “unsafe” can be used to declare a block of code that is not type-safe, allowing the use of pointers. The method of assigning pointers is shown as follows:

unsafe
{
    int x = 10;
    int* ptr = &x; // 赋值为变量x的地址

    Console.WriteLine(*ptr); // 输出变量x的值
}

It is important to note that enabling the ‘Allow unsafe code’ option in project properties is necessary when using pointers. Additionally, using pointers carries certain risks, so caution should be exercised.

bannerAds