get int value from enum c#

76

int value from enum in C# -

public enum Question
{
    Role = 2,
    ProjectFunding = 3,
    TotalEmployee = 4,
    NumberOfServers = 5,
    TopBusinessConcern = 6
}

int something = (int) Question.Role;

get int value from enum c# -

public class EnumValue
{
    public string Name { get; set; }
    public int Value { get; set; }
}

public static List<EnumValue> GetValues<T>()
{
  List<EnumValue> values = new List<EnumValue>();
  foreach (var itemType in Enum.GetValues(typeof(YourEnumClass)))
  {
    //For each value of this enumeration, add a new EnumValue instance
    values.Add(new EnumValue()
               {
                 Name = Enum.GetName(typeof(YourEnumClass), itemType), 
                 Value = (int)itemType
                 });
  }
  return values;
}

Comments

Submit
0 Comments