How can I cast int to enum?

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

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

30条回答
  •  北恋
    北恋 (楼主)
    2020-11-22 01:13

    I am using this piece of code to cast int to my enum:

    if (typeof(YourEnum).IsEnumDefined(valueToCast)) return (YourEnum)valueToCast;
    else { //handle it here, if its not defined }
    

    I find it the best solution.

提交回复
热议问题