Get int value from enum in C#

前端 未结 28 1958
臣服心动
臣服心动 2020-11-22 04:58

I have a class called Questions (plural). In this class there is an enum called Question (singular) which looks like this.

public e         


        
28条回答
  •  再見小時候
    2020-11-22 05:32

    public enum Suit : int
    {
        Spades = 0,
        Hearts = 1,
        Clubs = 2,
        Diamonds = 3
    }
    
    Console.WriteLine((int)(Suit)Enum.Parse(typeof(Suit), "Clubs"));
    
    // From int
    Console.WriteLine((Suit)1);
    
    // From a number you can also
    Console.WriteLine((Suit)Enum.ToObject(typeof(Suit), 1));
    
    if (typeof(Suit).IsEnumDefined("Spades"))
    {
        var res = (int)(Suit)Enum.Parse(typeof(Suit), "Spades");
        Console.Out.WriteLine("{0}", res);
    }
    

提交回复
热议问题