Get int value from enum in C#

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

    Use an extension method instead:

    public static class ExtensionMethods
    {
        public static int IntValue(this Enum argEnum)
        {
            return Convert.ToInt32(argEnum);
        }
    }
    

    And the usage is slightly prettier:

    var intValue = Question.Role.IntValue();
    

提交回复
热议问题