How to pass pointers as parameters to methods in C#?

To pass pointers as parameters to methods in C#, you need to use unsafe code and pointer syntax. Pointers allow direct memory manipulation and can be passed to methods for operations like swapping values or modifying data directly in memory.

C# requires the unsafe keyword when working with pointers, and your project must be configured to allow unsafe code compilation.

Syntax

Following is the syntax for declaring a method that accepts pointer parameters −

public unsafe void MethodName(int* pointer1, int* pointer2) {
    // pointer operations
}

Following is the syntax for calling a method with pointer arguments −

int value = 10;
int* ptr = &value;
MethodName(ptr, otherPtr);

Key Rules for Pointer Parameters

  • Methods accepting pointers must be declared with the unsafe modifier.

  • The calling method must also be marked unsafe if it uses pointer operations.

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

  • Use the dereference operator * to access the value at a pointer address.

Pointer Parameter Passing Main Method int var1 = 10; int* x = &var1; swap(x, y); swap Method int temp = *p; *p = *q; *q = temp; Pass pointers Memory addresses are passed, allowing direct modification of original variables through pointer dereferencing

Using Pointer Parameters to Swap Values

Example

using System;

class Program {
    public unsafe void swap(int* p, int* q) {
        int temp = *p;
        *p = *q;
        *q = temp;
    }

    public unsafe static void Main() {
        Program program = new Program();
        int var1 = 10;
        int var2 = 20;
        int* x = &var1;
        int* y = &var2;

        Console.WriteLine("Before Swap: var1:{0}, var2: {1}", var1, var2);
        program.swap(x, y);
        Console.WriteLine("After Swap: var1:{0}, var2: {1}", var1, var2);
    }
}

The output of the above code is −

Before Swap: var1:10, var2: 20
After Swap: var1:20, var2: 10

Using Pointer Parameters for Array Manipulation

Example

using System;

class Program {
    public unsafe void ModifyArray(int* arr, int size) {
        for (int i = 0; i < size; i++) {
            *(arr + i) = *(arr + i) * 2;
        }
    }

    public unsafe static void Main() {
        Program program = new Program();
        int[] numbers = {1, 2, 3, 4, 5};
        
        Console.WriteLine("Original array:");
        foreach (int num in numbers) {
            Console.Write(num + " ");
        }
        Console.WriteLine();

        fixed (int* ptr = numbers) {
            program.ModifyArray(ptr, numbers.Length);
        }

        Console.WriteLine("Modified array:");
        foreach (int num in numbers) {
            Console.Write(num + " ");
        }
        Console.WriteLine();
    }
}

The output of the above code is −

Original array:
1 2 3 4 5 
Modified array:
2 4 6 8 10

Conclusion

Passing pointers as parameters in C# requires unsafe code and enables direct memory manipulation. This approach allows methods to modify original variables through memory addresses, providing efficient data manipulation for scenarios like swapping values or array operations.

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

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements