繁体   English   中英

为什么不能将Func设置为将参数类型返回为整数值?

[英]Why can I not set a Func to return a parameter type to an integer value?

如果Func返回值,为什么不能简单地将其设置为与Func <.... Parameter List ....>中的最后一个参数相同的返回类型。

问题:为什么不能将Func设置为返回类型?

还是我必须使用上面声明的变量returnResult?

都不起作用。

码:

int tempValueResult = Func<int, int, int, int, int, int, int> myMeth6 = (a, b, c, y, e, returnResult) =>
{
    Console.WriteLine(a + b + c + y + e);
    return (a);
};

Console.WriteLine(returnResult);

要么 -

int returnResult = 0;
Func<int, int, int, int, int, int, int> myMeth6 = (a, b, c, y, e, returnResult) =>
{
    Console.WriteLine(a + b + c + y + e);
    return (a);
};

为什么不能将Func设置为返回类型?

因为返回int函数函数 ,而不是int 给定特定输入的函数结果int

在两个示例中,您都没有为输入提供任何值,因此没有可取回的值。 直到您提供输入并评估功能后,您才能获得有意义的结果。

您的第二个示例工作正常,但是您必须提供输入并捕获输出:

returnResult = myMeth6(1, 2, 3, 4, 5);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM