How can I cast int to enum?

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

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

30条回答
  •  遥遥无期
    2020-11-22 01:17

    enter image description here

    To convert a string to ENUM or int to ENUM constant we need to use Enum.Parse function. Here is a youtube video https://www.youtube.com/watch?v=4nhx4VwdRDk which actually demonstrate's with string and the same applies for int.

    The code goes as shown below where "red" is the string and "MyColors" is the color ENUM which has the color constants.

    MyColors EnumColors = (MyColors)Enum.Parse(typeof(MyColors), "Red");
    

提交回复
热议问题