O'Reilly logo

From Java to C#: A Developer's Guide by Heng Ngee Mok

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

29.8. Passing pointers to methods

Let's put together what we have learnt so far, and take a look at a general example of how pointers are passed between methods.

Examine this program:

 1: using System;
 2:
 3: class TestClass{
 4:
 5:   static unsafe void PerformOp (int* pX){        <-- 1
 6:     *pX = 99; 
						<-- 2
 7:   }
 8:
 9:   public static unsafe void Main(){
10:     int a = 1;
11:     Console.WriteLine("a was :" + a);
12:     PerformOp (&a); 
						<-- 3
13:     Console.WriteLine("a is :" + a);
14:   }
15: }

(1)Method takes in a parameter of type int*, a pointer

(2)Uses indirection to change the int value stored at the address in pX

(3)Invoke method by passing in the address of an int

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required