Information Technology Reference
In-Depth Information
Figure 15-9.
The return value of the last method executed is the value returned by the delegate.
Invoking Delegates with Reference Parameters
If a delegate has a reference parameter, the value of the parameter can change upon return
from one or more of the methods in the invocation list.
When calling the next method in the invocation list, the
new value of the parameter
is the
one passed to the next method.
For example, the following code invokes a delegate with a reference parameter. Figure 15-10
illustrates the code.
delegate void MyDel( ref int X );
class MyClass {
public void Add2(ref int x) { x += 2; }
public void Add3(ref int x) { x += 3; }
static void Main(string[] args)
{
MyClass mc = new MyClass();
MyDel mDel = mc.Add2;
mDel += mc.Add3;
mDel += mc.Add2;
int x = 5;
mDel(ref x);
Console.WriteLine("Value: {0}", x);
}
}