What are pointers in C#?

Pointer is a variable whose value is the address of another variable i.e., the direct address of the memory location.

The syntax of a pointer is −

type *var-name;

The following is how you can declare a pointer type −

double *z; /* pointer to a double */

C# allows using pointer variables in a function of code block when it is marked by the unsafe modifier. The unsafe code or the unmanaged code is a code block that uses a pointer variable.

The following is our module showing how to declare and use a pointer variable. We have used unsafe modifier here −

static unsafe void Main(string[] args) {
   int val = 50;
   int* x = &val;

   Console.WriteLine("Data: {0} ", val);
   Console.WriteLine("Address: {0}", (int)x);

Updated on: 20-Jun-2020

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started