Retrieve data value as a pointer in C#


A pointer is a variable whose value is the address of another variable. Retrieve the data stored at the located referenced by the pointer variable, using the ToString() method.

Example

Here in an example −

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();
      }
   }
}

Output

Above will require you to set unsafe comman line option. After seeting it, the following output would be visible.

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

Updated on: 22-Jun-2020

570 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements