Retrieve data value as a pointer in C#

CsharpProgrammingServer Side Programming

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
raja
Published on 16-Aug-2018 12:11:53
Advertisements