2014-10-19 79 views
-8

我需要幫助添加一個私有方法,返回一個布爾類型整數。 如果參數是< 50,那麼它應該返回值false,否則返回C#私有方法布爾

我目前正在與C#的實驗,並使用if else語句,但需要使用獲取,設置

private static bool AmHungry 
if size < 50; 
{ 
    return false; 
} 
else 
{ 
    return true; 
} 

我已經嘗試使用get set訪問器,但我怎麼能夠定義需要滿足的值來決定布爾答案?

回答

1

爲了定義任何值,您需要將其保存到另一個變量中。爲了使用它,您然後調用該變量。

public static int HungerThreshold { get; set; } // Using this creates anthe imlicit field and simple access at compile time 
publis static bool AmHungry 
{ 
    get { return size < HungerThreshold; } // Forget not that compaison operators return bool, and can thus be returned directly instead of using an if/else 
} 

另外不要忘記,方法總是需要(){},和屬性總是需要{}