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

14.6. Passing method parameters by reference in delegates

It is possible for methods to take in parameters by reference instead of by value using either the ref or out keywords (see section 7.2).

In the case of a composite delegate instance, the methods in the invocation list are invoked in sequence. Since each method is invoked with the same set of parameters as that given to the delegate instance, for passed-by-reference parameters, each method invocation will be passed the same reference. This means that changes to that variable by one method in the invocation list will be passed in to the remaining methods. Study the example below:

 1:  using System;
 2:
 3:  delegate void MyDelegate (ref int i); 4: 5: class TestClass{ 6: 7: static void MyMethod( ...

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