Man*_*lim -1 c# asp.net asp.net-mvc enums
例如,我enum在C#中声明了以下内容。
C#代码:
public enum LobMaster :int
{
RetailBroking = 1,
HNI = 2,
TechnologyInfrastructure = 3,
}
Run Code Online (Sandbox Code Playgroud)
cshtml代码:
@int code = 2
@if (LobMaster.RetailBroking == code)
{
}
Run Code Online (Sandbox Code Playgroud)
如果我尝试将int枚举与整数值进行比较,则会显示以下错误消息。
运算符'=='不能应用于类型'LobMaster'和'int'
虽然我的枚举
龙虾大师
已经是int类型。
但是,当我将枚举转换为int并与int值进行比较时,它可以正常工作。所以我的问题是,为什么我需要将enum强制转换为int,因为enum已经是一个int了。
枚举的主要目的是删除“魔术数字”,因此您应该问自己-为什么要使用那些魔术数字?
更好的比较是:
Session["Lob_code"] is LobMaster && LobMaster.MassAffluent == (LobMaster)Session["Lob_code"]
Run Code Online (Sandbox Code Playgroud)
这样,您就可以避免那些魔术数字,而您的代码仅提供所有上下文。