所以我搜索了无数次,只会越来越困惑。我有一个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.Hourly
是enum
PayType
的成员,而this.PayType
(this.
是可选的,但为了清楚起见包括在内)是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)