【发布时间】:2013-08-25 07:46:40
【问题描述】:
我有一个这样定义的枚举类型:
public enum Status
{
Active=1,
InActive=0
}
在我的方法中,我可以像这样将参数转换为枚举:
public string doSomething(string param1, int status)
{
//will this work?
Account.Status = (Status) status;
//or do i need to test one by one like this
if(status == 0)
{
Account.Status = Status.Inactive;
//and so on...
} // end if
} // end doSomething
【问题讨论】:
-
为什么不直接运行代码并找出答案。
-
@Kaf 可能的原因:他使用的数据库支持
int,但不支持enum。 -
是的,它会起作用的。但是,最好先检查 int 是否在声明的枚举范围内。
-
我运行了代码...编译器没有抱怨。当传入不在枚举中的值时会发生什么?
-
@sirbombay 你应该可以很容易地尝试一下......