enum MyEnum
{
Invalid=0,
Value1=1,
Value1=2,
}
void main ()
{
MyEnum e1 = MyEnum.Value1;
int i1 = 2;
// Is there any difference how to compare enumEration values with integers?
if ( e1==(MyEnum)i1 )... // 1st
if ( (int)e1==i1 )... // 2nd
在上述每种情况下,我们都将枚举转换为 int 或将 int 转换为枚举。
这些转化(性能,任何其他)有什么不同吗?或者它们完全一样?
谢谢。
附言在当前示例中,我将其与“魔数(Magic Number)”进行比较,但在实际应用中,我从数据库的整数字段中获取数据。
最佳答案
这会有所帮助。
var constant = 1;
if(constant == (int)MyEnum.Valid1){
......
}
关于c# - 如何比较枚举值和整数值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5276100/