簡體   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