Get int value from enum in C#

get int value of enum java
c# get enum value from string
c# enum
enum to int c++
get enum value java
c# enum to list key value
c# enum string value
c# assign enum value from int

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

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

In the Questions class I have a get(int foo) function that returns a Questions object for that foo. Is there an easy way to get the integer value off the enum so I can do something like this Questions.Get(Question.Role)?

Just cast the enum, e.g.

int something = (int) Question.Role;

The above will work for the vast majority of enums you see in the wild, as the default underlying type for an enum is int.

However, as cecilphillip points out, enums can have different underlying types. If an enum is declared as a uint, long, or ulong, it should be cast to the type of the enum; e.g. for

enum StarsInMilkyWay:long {Sun = 1, V645Centauri = 2 .. Wolf424B = 2147483649};

you should use

long something = (long)StarsInMilkyWay.Wolf424B;

Learn C#, Firstly, set the enum −public enum Vehicle { Car, Bus, Truck }Now use typecasting to cast an enum to int −int a = (int)Vehicle.Car;The following� Getting an int value from enum can be challenging in C#. However, it can easily be done by using the following code: public enum Months { Numbers = 23, MonthType = 3 } int abc = (int)Months.Number;

Since Enums can be any integral type (byte, int, short, etc.), a more robust way to get the underlying integral value of the enum would be to make use of the GetTypeCode method in conjunction with the Convert class:

enum Sides {
    Left, Right, Top, Bottom
}
Sides side = Sides.Bottom;

object val = Convert.ChangeType(side, side.GetTypeCode());
Console.WriteLine(val);

This should work regardless of the underlying integral type.

How to get int value from enum in C#?, WriteLine("The values of the Colors Enum are:"); foreach(int i in Enum. GetFields method to get an array of FieldInfo objects that represent enumeration � Following is the method in C# to get the enum value by int. /// /// Method to get enumeration value from int value. /// /// /// public T GetEnumValue<T> (int intValue) where T : struct, IConvertible { if (!typeof(T).IsEnum) { throw new Exception("T must be an Enumeration type.");

Declare it as a static class having public constants:

public static class Question
{
    public const int Role = 2;
    public const int ProjectFunding = 3;
    public const int TotalEmployee = 4;
    public const int NumberOfServers = 5;
    public const int TopBusinessConcern = 6;
}

And then you can reference it as Question.Role, and it always evaluates to an int or whatever you define it as.

Enum.GetValues(Type) Method (System), Visual C#. Visual C# But the compiler errors when I cast the Enum directly to an int. Enum' to 'int' intValue = (int)enumValue; Debug. 1. Enum.GetName Method. The standard method to retrieve the name of the constant having the specified value is to use Enum.GetName method. This is demonstrated below. 2. Casting. To get the corresponding constant value from an enumeration member, use casting. The following example demonstrates its usage. Loading

Question question = Question.Role;
int value = (int) question;

Will result in value == 2.

How to cast an Enum directly to int? - MSDN, How to get integer value from enum member name in C# We can get the constant integer value simply by casting the enum member int value = (int) code;. Instead, you can retrieve the value of all enumeration members by using the Type.GetFields method to get an array of FieldInfo objects that represent enumeration members and then call the FieldInfo.GetRawConstantValue method on each element of the array. The following example illustrates this technique.

On a related note, if you want to get the int value from System.Enum, then given e here:

Enum e = Question.Role;

You can use:

int i = Convert.ToInt32(e);
int i = (int)(object)e;
int i = (int)Enum.Parse(e.GetType(), e.ToString());
int i = (int)Enum.ToObject(e.GetType(), e);

The last two are plain ugly. I prefer the first one.

How to get integer value from enum member name in C#, It is useful when you have a set of values that are functionally significant and enum Days { Sunday = 1, TuesDay = 2, wednesday=3 } //get value int day� What is the difference between the following: (1)Enum.GetValues (typeof (theEnum)); // returns the pure enumeration defined in Enum (2)int i = (int) theEnum.item; // Indicator number from zero if not set and if they are set their value---- (3)Enum.GetNames (typeof (theEnum)); // Enumeration name returns as string – zokaee hamid Nov 9 '18 at 16:40

How to use Enum in C# - CSharp, This tutorial explains enum in C#. If values are not assigned to enum members, then the compiler will assign Friday; // enum to int conversion Console. This works when you have an enum, but you don't have the type of enum. E.g. void DoSomethingWithEnum(Enum value) { int X = Convert.ToInt32(value); } In this case a normal cast to int doesn't work. – Martin Lottering Mar 20 '13 at 15:04

C# Enumerations Type - Enum, In the Questions class, I have a get(int foo) function that returns a Questions object for that foo . Is there an easy way to get the integer value from the enum so I� By default the underlying type of each element in the enum is integer. enum Values { A, B, C } You can also specify custom value for each item: enum Values { A = 10, B = 11, C = 12 } int x = (int)Values.A; // x will be 10; Note: By default, the first enumerator has the value 0.

Get int value from enum in C#, The default underlying type for enums in C# is Int. So we can cast the enum to int to get the int value from enum in C#. We will take an example� Since Enums can be any integral type (short, byte, int, etc.), a more robust way to get the underlying integral value of the enum would be to make use of the GetTypeCode method in conjunction with the Convert class

Comments
  • For the other way around: cast-int-to-enum-in-c-sharp.
  • I know I'm late to the party, but instead of defining your method as get(int foo) you can define it as get(Question foo) then do your casting inside the method, the you can call your method as Questions.Get(Question.Role)
  • @Harry it isn't true. You can create Enumeration without casting, it is not required. and I only assign number in special cases, most of the time, I leave it as default value. but you can do enum Test { Item = 1 } and see that 1 == (int)Test.Item is equal.
  • @Jaider (int)Test.Item That is a cast! () is the explicit cast operator.
  • @Sinthia V he said you can create it without casting, which is correct
  • If the underlying type for enum Question was not int but long this cast will truncate Roles integral value!
  • When you accept an Enum as a parameter, you know is only a fixed number of possible integral values you can get. On the other hand, if you take simply an int, then you have to validate if that int is within the accepted values., thus complicating the code. You can always override your signatures like ``` public void MyMethod(int x) { // do something with x } public void MyMethod(Enum x) { this.MyMethod((int) x); } ````
  • This technique proved its worth to me when dealing with a generic type where T:enum (actually T:struct, IConvertible but that's a different story).
  • How would you modify this to print out the hexadecimal value of side? This example shows the decimal value. The problem is that var is of type object, so you need to unbox it and it gets messier than I would like.
  • I think you should change the example to object val = Convert...etc the var in your example will always be object.