What are pointers in C#?

CsharpProgrammingServer Side Programming

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);
   Console.ReadKey();
}
raja
Published on 09-Aug-2018 08:40:40
Advertisements