Func delegate doesn't chain methods

前端 未结 2 839
执笔经年
执笔经年 2020-12-31 11:06

Lets imagine simple delegate calls:

void Main()
{
    Func tfunc = null;
    tfunc += Add; // bind first method
    tfunc += Sub; //          


        
相关标签:
2条回答
  • 2020-12-31 11:39

    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.

    0 讨论(0)
  • 2020-12-31 11:45

    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.

    0 讨论(0)
提交回复
热议问题