Reference Parameters

Although assignment to a value parameter is visible only to the current method and not to its caller, reference parameters can circumvent this. Instead of making a copy of the object (whatever that means is dependent on the value- or reference-type characteristic of the object, as you learned in Chapter 9), a reference is made to the local variable held by the caller. Figure 10.5 illustrates the use of the ref modifier to create a reference parameter. Contrast the diagram with that shown earlier in Figure 10.3 to see the difference.

Now inside the Foo method we have a reference to the local variable held in the context of the calling Bar method. Any assignment to the reference parameter will happen to the original location ...

Get C# 5.0 Unleashed now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.