How to get enum value by string or int

后端 未结 10 1254
慢半拍i
慢半拍i 2020-12-07 12:43

How can I get the enum value if I have the enum string or enum int value. eg: If i have an enum as follows:

public enum TestEnum
{
    Value1 = 1,
    Value         


        
10条回答
  •  情歌与酒
    2020-12-07 13:20

    Here is an example to get string/value

        public enum Suit
        {
            Spades = 0x10,
            Hearts = 0x11,
            Clubs = 0x12,
            Diamonds = 0x13
        }
    
        private void print_suit()
        {
            foreach (var _suit in Enum.GetValues(typeof(Suit)))
            {
                int suitValue = (byte)(Suit)Enum.Parse(typeof(Suit), _suit.ToString());
                MessageBox.Show(_suit.ToString() + " value is 0x" + suitValue.ToString("X2"));
            }
        }
    

        Result of Message Boxes
        Spade value is 0x10
        Hearts value is 0x11
        Clubs value is 0x12
        Diamonds value is 0x13
    

提交回复
热议问题