12

Lets imagine simple delegate calls:

void Main()
{
    Func<int, int, string> tfunc = null;
    tfunc += Add; // bind first method
    tfunc += Sub; // bind second method 

    Console.WriteLine(tfunc(2, 2));
}

private string Add(int a, int b)
{
    return "Add: " + (a + b).ToString();
}

private string Sub(int a, int b)
{
    return "Sub: " + (a - b).ToString();
}

The result of this program is:

Sub: 0

So, why Add method was not called? I'm expecting to call Method Add, and then method Sub.

Dariusz
  • 15,573
  • 9
  • 52
  • 68
  • 5
    When calling a delegate that has a return value, only the last executed delegate returns a value to the invoking code. In this case, `Add` which is called first has its return value thrown away by the `Sub` delegate which executes afterward. – Chris Sinclair Jan 22 '13 at 16:37

2 Answers2

16

Add was correctly chained and called, take a look at the result of

void Main()
{
    Func<int, int, string> tfunc = null;
    tfunc += Add; // bind first method
    tfunc += Sub; // bind second method 

    Console.WriteLine(tfunc(2, 2));
}

private string Add(int a, int b)
{
    Console.WriteLine("Inside Add");
    return "Add: " + (a + b).ToString();
}

private string Sub(int a, int b)
{
    Console.WriteLine("Inside Sub");
    return "Sub: " + (a - b).ToString();
}

It is:

Inside Add
Inside Sub
Sub: 0

What is not chained, because there is no way to access it, is the result of the Add method. Delegates that return a value, in case of chaining, return the value of the last method invoked, that is the last method that was added to the delegate.

This is specified in part 15.4 of the C# 4.0 language specification

Invocation of a delegate instance whose invocation list contains multiple entries proceeds by invoking each of the methods in the invocation list, synchronously, in order. ... If the delegate invocation includes output parameters or a return value, their final value will come from the invocation of the last delegate in the list.

SWeko
  • 30,434
  • 10
  • 71
  • 106
3

The problem is that the return value is not passed between the method invocations, so the output only captures the last string returned. I.e. the return of Add is lost.

Pang
  • 9,564
  • 146
  • 81
  • 122
Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317