返回基于枚举选择的布尔值



所以我搜索了无数次,只会越来越困惑。我有一个C#MVC应用程序,其中有一个类"Income"。我在下面输入了我想要实现的目标,但似乎无法理解。

public enum PayType
{
    Hourly, Salary, Commission
}
public class Income
{
    public PayType PayType {get; set;}
    public bool IsOvertimeEligible 
        { get 
            { if(PayType.Hourly)
                 {
                      return true;
                 }
             return false;
            }
         }
}

我尝试过"typeof"和其他一些东西,但似乎无法摆脱零。

如有任何帮助,将不胜感激

PayType.Hourlyenum PayType的成员,而this.PayTypethis.是可选的,但为了清楚起见包括在内)是class Income的成员。这两者都不能用作if语句的条件,但两者的相等比较(==)也可以作为bool语句的条件。因此,更改

if(PayType.Hourly)

if (this.PayType == PayType.Hourly)

以进行编译。


您可以通过去掉多余的if语句来简化get调用

public bool IsOvertimeEligible
{
    get { return this.PayType == PayType.Hourly; }
}

if (condition)
   return true;
else
   return false;

可以只是

return condition;

这:

public bool IsOvertimeEligible
{
    get
    {
        return this.PayType == PayType.Hourly;
    }
}

试试这个:

if(PayType == PayType.Hourly)

最新更新