What are pointers in C#?

A pointer is a variable that stores the memory address of another variable. Unlike reference types, pointers provide direct access to memory locations, making them powerful but potentially unsafe.

In C#, pointers can only be used within unsafe code blocks or methods. This restriction exists because pointers bypass .NET's garbage collection and type safety mechanisms.

Syntax

Following is the syntax for declaring a pointer −

type *pointerName;

Following is the syntax for getting the address of a variable −

type *pointer = &variable;

Following is the syntax for dereferencing a pointer −

type value = *pointer;

Declaring and Using Pointers

Pointers must be used within unsafe code blocks. Here's how to declare different types of pointers −

int *intPtr;       // pointer to an integer
double *doublePtr; // pointer to a double
char *charPtr;     // pointer to a character

Example

using System;

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

      Console.WriteLine("Data: {0}", val);
      Console.WriteLine("Address: {0}", (long)x);
      Console.WriteLine("Value at address: {0}", *x);

      // Modify value through pointer
      *x = 100;
      Console.WriteLine("Modified value: {0}", val);
   }
}

The output of the above code is −

Data: 50
Address: 2028580716
Value at address: 50
Modified value: 100

Pointer Arithmetic

You can perform arithmetic operations on pointers to navigate through memory −

Example

using System;

class Program {
   static unsafe void Main(string[] args) {
      int[] numbers = {10, 20, 30, 40, 50};
      
      fixed (int* ptr = numbers) {
         Console.WriteLine("Array elements using pointer arithmetic:");
         for (int i = 0; i < 5; i++) {
            Console.WriteLine("Element {0}: {1}", i, *(ptr + i));
         }
      }
   }
}

The output of the above code is −

Array elements using pointer arithmetic:
Element 0: 10
Element 1: 20
Element 2: 30
Element 3: 40
Element 4: 50

Using 'fixed' Statement

The fixed statement prevents the garbage collector from relocating movable variables during pointer operations −

Example

using System;

class Program {
   static unsafe void Main(string[] args) {
      string text = "Hello";
      
      fixed (char* ptr = text) {
         Console.WriteLine("Characters in string:");
         for (int i = 0; i < text.Length; i++) {
            Console.WriteLine("Character {0}: {1}", i, *(ptr + i));
         }
      }
   }
}

The output of the above code is −

Characters in string:
Character 0: H
Character 1: e
Character 2: l
Character 3: l
Character 4: o

Key Rules and Limitations

  • Pointers can only be used within unsafe code blocks or methods.

  • You must compile with the /unsafe compiler option.

  • Pointers can only point to unmanaged types (value types, not reference types).

  • Use the fixed statement when working with managed arrays or strings.

  • Pointer arithmetic follows the size of the pointed type.

Comparison with References

Pointers References
Store actual memory addresses Managed by .NET runtime
Require unsafe code Type-safe by default
Allow direct memory manipulation Automatic garbage collection
Can perform pointer arithmetic No arithmetic operations allowed

Conclusion

Pointers in C# provide direct memory access but require unsafe code blocks. They are mainly used for performance-critical applications, interoperability with unmanaged code, and scenarios requiring precise memory control. Use pointers cautiously as they bypass .NET's safety mechanisms.

Updated on: 2026-03-17T07:04:35+05:30

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements