How can I cast int to enum?

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

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

30条回答
  •  花落未央
    2020-11-22 01:22

    I need two instructions:

    YourEnum possibleEnum = (YourEnum)value; // There isn't any guarantee that it is part of the enum
    if (Enum.IsDefined(typeof(YourEnum), possibleEnum))
    {
        // Value exists in YourEnum
    }
    

提交回复
热议问题