[英]What are the ways I can write this differently? - static bool M (int x) => x%2 == 0;
我目前正在學習測試,我已經查看了 Function、Action 和 Predicate 的意思,我知道只有 Action 沒有返回值,而 Predicate 返回一個 bool 值,這是我在問題中需要的。 這是我在問題中得到的代碼:
static bool M (int x) => x%2 == 0;
這些是給出的潛在答案:
A) Func <bool, int> A = M;
B) Func <int, bool> B = M;
C) Action <int, bool> C = M;
D) Predicate <int> D = M;
根據我所學和一些簡單的邏輯,它應該是 B 和 D,對嗎? 有人可以幫我確認一下嗎?
您可以通過它的 class 來呼叫 static function。例如:
public class yourClass
{
> public static void DoSomething(bool isTrue)
> {
> //Here you can do some things
> }
}
yourClass.DoSomething(true);
如果您想要 function 返回一些值,例如 bool、string 等
Public class yourClass
{
>public static bool DoSomething(bool isTrue)
>{
>>if(isTrue)
>>{
>>return false;
>>}
>>else
>>{
>>return true;
>>}
>}
}
bool isItTrue = yourClass.DoSomething(true);
返回值的 function 必須始終具有返回值。 如果您從上面的示例中刪除 else 部分,您將得到一個錯誤,即並非所有代碼都返回一個值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.