Func delegate doesn't chain methods

蓝咒 提交于 2019-12-18 13:07:16

问题


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.



来源:https://stackoverflow.com/questions/14463400/func-delegate-doesnt-chain-methods

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!