Get int value from enum in C#

前端 未结 28 1916
臣服心动
臣服心动 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:31

    One more way to do it:

    Console.WriteLine("Name: {0}, Value: {0:D}", Question.Role);
    

    It will result in:

    Name: Role, Value: 2
    
    0 讨论(0)
  • 2020-11-22 05:31

    Use:

    Question question = Question.Role;
    int value = Convert.ToInt32(question);
    
    0 讨论(0)
  • 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);
    }
    
    0 讨论(0)
  • 2020-11-22 05:33

    It's easier than you think - an enum is already an int. It just needs to be reminded:

    int y = (int)Question.Role;
    Console.WriteLine(y); // Prints 2
    
    0 讨论(0)
  • 2020-11-22 05:33

    Example:

    public enum EmpNo
    {
        Raj = 1,
        Rahul,
        Priyanka
    }
    

    And in the code behind to get the enum value:

    int setempNo = (int)EmpNo.Raj; // This will give setempNo = 1
    

    or

    int setempNo = (int)EmpNo.Rahul; // This will give setempNo = 2
    

    Enums will increment by 1, and you can set the start value. If you don't set the start value it will be assigned as 0 initially.

    0 讨论(0)
  • 2020-11-22 05:34

    I came up with this extension method that includes current language features. By using dynamic, I don't need to make this a generic method and specify the type which keeps the invocation simpler and consistent:

    public static class EnumEx
    {
        public static dynamic Value(this Enum e)
        {
            switch (e.GetTypeCode())
            {
                case TypeCode.Byte:
                {
                    return (byte) (IConvertible) e;
                }
    
                case TypeCode.Int16:
                {
                    return (short) (IConvertible) e;
                }
    
                case TypeCode.Int32:
                {
                    return (int) (IConvertible) e;
                }
    
                case TypeCode.Int64:
                {
                    return (long) (IConvertible) e;
                }
    
                case TypeCode.UInt16:
                {
                    return (ushort) (IConvertible) e;
                }
    
                case TypeCode.UInt32:
                {
                    return (uint) (IConvertible) e;
                }
    
                case TypeCode.UInt64:
                {
                    return (ulong) (IConvertible) e;
                }
    
                case TypeCode.SByte:
                {
                    return (sbyte) (IConvertible) e;
                }
            }
    
            return 0;
        }
    
    0 讨论(0)
提交回复
热议问题