-->

Func delegate doesn't chain methods

2020-02-26 07:56发布

问题:

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.

回答1:

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.



回答2:

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