Retrieve data value as a pointer in C#

A pointer is a variable whose value is the address of another variable. In C#, pointers can only be used in unsafe contexts. To retrieve the data stored at the location referenced by the pointer variable, you can use the dereference operator * or call the ToString() method on the pointer.

Syntax

Following is the syntax for declaring and using pointers in C# −

unsafe {
    int* ptr = &variable;     // Declare pointer and get address
    int value = *ptr;         // Dereference to get value
    string str = ptr->ToString(); // Get value as string
}

Key Rules for Pointers

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

  • The project must be compiled with the /unsafe compiler option.

  • Use & operator to get the address of a variable.

  • Use * operator to dereference a pointer and get its value.

Pointer Memory Layout Variable var = 100 Address: 0x1234 Pointer p = 0x1234 Points to var stores address *p retrieves value: 100 p->ToString() returns "100"

Using Pointers to Retrieve Data

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 via pointer: {0} ", *p);
                Console.WriteLine("Data using ToString(): {0} ", p->ToString());
                Console.WriteLine("Address is: {0} ", (int)p);
            }
        }
    }
}

The output of the above code is −

Data is: 100 
Data via pointer: 100 
Data using ToString(): 100 
Address is: 77678547

Multiple Pointer Operations

Example

using System;

namespace PointerExample {
    class Program {
        public static void Main() {
            unsafe {
                double num1 = 25.5;
                double num2 = 42.8;
                
                double* ptr1 = &num1;
                double* ptr2 = &num2;

                Console.WriteLine("Value at ptr1: " + ptr1->ToString());
                Console.WriteLine("Value at ptr2: " + ptr2->ToString());
                
                // Modify values through pointers
                *ptr1 = 30.7;
                *ptr2 = 55.3;
                
                Console.WriteLine("Modified value at ptr1: " + (*ptr1).ToString());
                Console.WriteLine("Modified value at ptr2: " + (*ptr2).ToString());
            }
        }
    }
}

The output of the above code is −

Value at ptr1: 25.5
Value at ptr2: 42.8
Modified value at ptr1: 30.7
Modified value at ptr2: 55.3

Compiler Configuration

To compile unsafe code, you need to enable the unsafe option. In Visual Studio, go to Project Properties ? Build ? Check "Allow unsafe code". For command line compilation, use the /unsafe flag −

csc /unsafe Program.cs

Conclusion

Pointers in C# allow direct memory access within unsafe contexts. You can retrieve pointer data using the dereference operator * or the ToString() method. Remember to compile with the /unsafe option and use pointers judiciously as they bypass .NET's memory safety features.

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

968 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements