Get Int Value from Enum in C#

Get int value from enum in C#

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;

How to get integer values from Enum in c#

Basically you cam use Enum.Parse to parse the string and then cast to int like below :

var str  = "value1";
var value = (int)Enum.Parse<MyEnum>(str);

Fiddle

Getting the integer value from enum

Just parse the string and cast to int.

var number = (int)((game) Enum.Parse(typeof(game), pick));

How do I cast int to enum in C#?

From an int:

YourEnum foo = (YourEnum)yourInt;

From a string:

YourEnum foo = (YourEnum) Enum.Parse(typeof(YourEnum), yourString);

// The foo.ToString().Contains(",") check is necessary for
// enumerations marked with a [Flags] attribute.
if (!Enum.IsDefined(typeof(YourEnum), foo) && !foo.ToString().Contains(","))
{
throw new InvalidOperationException(
$"{yourString} is not an underlying value of the YourEnum enumeration."
);
}

From a number:

YourEnum foo = (YourEnum)Enum.ToObject(typeof(YourEnum), yourInt);

display int value from enum

To get all values of the Enum try this

var allValues = Enum.GetValues(typeof(TotalSteps)).Cast<int>().ToArray();

and your totalSteps property should look like this

public int[] totalSteps
{
get { return Enum.GetValues(typeof(TotalSteps)).Cast<int>().ToArray(); }
}

How to convert enum to int

There are plenty of other ways (including Convert.ToInt32 as mentioned by acrilige), but a static cast is probably the best choice (as far as readability and performance are concerned)

How to get the int for enum value in Enumeration

To answer your question "how can I get the integer for string value":

To convert from a string into an enum, and then convert the resulting enum to an int, you can do this:

public enum Animals
{
dog = 0,
cat = 1,
rat = 2
}

...

Animals answer;

if (Enum.TryParse("CAT", true, out answer))
{
int value = (int) answer;
Console.WriteLine(value);
}

By the way, normal enum naming convention dictates that you should not pluralize your enum name unless it represents flag values (powers of two) where multiple bits can be set - so you should call it Animal, not Animals.

Get enum int value by string

Please try with the below code snippet.

public void setCaseOriginCode(string CaseOriginCode)
{
int caseOriginCode = (int)(CaseOriginCode)Enum.Parse(typeof(CaseOriginCode), CaseOriginCode);
}

Let me know if any concern.

How to print an Enum Value based on an integer user input?

You can simply cast the value to the enum. Don't forget to set initial value for January or take into account that by default enum starts from 0;

Console application will be next:

class Program
{

public enum Month
{
January, February, March,
April, May, June, July, August,
Septemper, October, November, December
};

static void Main(string[] args)
{
Console.WriteLine("Enter the number of the month");

int monthValue = 0;
int.TryParse(Console.ReadLine(), out monthValue);
Console.WriteLine((Month)monthValue - 1);
Console.ReadKey();
}
}

in case you don't need temporary variable you could also really convert it to enum. But not forget to set default enum value

    public enum Month
{
January = 1, February, March,
April, May, June, July, August,
Septemper, October, November, December
};

static void Main(string[] args)
{
Console.WriteLine("Enter the number of the month");
var input = Enum.Parse(typeof(Month), Console.ReadLine());
Console.WriteLine(input);
Console.ReadKey();
}


Related Topics



Leave a reply



Submit