c# enum to int code example

Example 1: C# .net core convert int to enum

if (Enum.IsDefined(typeof(YourEnum), yourInt))
{
    return (YourEnum) yourInt;
}

  OR:

if (Enum.IsDefined(typeof(YourEnum), yourInt))
{
    return (YourEnum) Enum.ToObject(typeof(YourEnum), yourInt);
}

Example 2: c# enum to int

Just cast the enum, e.g.
int something = (int) WeaponType.BFG;

Example 3: convert int to enum c#

YourEnum foo = (YourEnum)yourInt;

Example 4: c# enum syntax

enum State
{
	Idle,
    Walking,
    Running,
    Jumping,
    Climbing,
    Attacking
}

Example 5: enums c#

enum Season
{
    Spring,
    Summer,
    Autumn,
    Winter
}

Example 6: get enum value c#

int something = (int) Question.Role;