c# cast enum to value 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# get enum value from string
//This example will parse a string to a Keys value
Keys key = (Keys)Enum.Parse(typeof(Keys), "Space");
//The key value will now be Keys.Space