在C#中,将数据值作为指针检索

来源:这里教程网 时间:2026-02-21 16:33:30 作者:

在c#中,将数据值作为指针检索

指针是一个变量,其值是另一个变量的地址。使用ToString()方法检索指针变量所引用的位置存储的数据。

示例

以下是一个示例 -

using System;
namespace UnsafeCodeApplication {
   class Program {
      public static void Main() {
         unsafe {
            int var = 100;
            int* p = &var;
            Console.WriteLine("Data is: {0} " , var);
            Console.WriteLine("Data is: {0} " , p->ToString());
            Console.WriteLine("Address is: {0} " , (int)p);
         }
         Console.ReadKey();
      }
   }
}

输出

上述操作需要您设置不安全的命令行选项。在设置完成后,将会显示以下输出。

Data is: 100
Data is: 100
Address is: 77678547

相关推荐