How can I cast int to enum?

后端 未结 30 1488
礼貌的吻别
礼貌的吻别 2020-11-22 00:56

How can an int be cast to an enum in C#?

30条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 01:17

    Just cast it:

    MyEnum e = (MyEnum)3;
    

    You can check if it's in range using Enum.IsDefined:

    if (Enum.IsDefined(typeof(MyEnum), 3)) { ... }
    

提交回复
热议问题