How can an int be cast to an enum in C#?

How can an int be cast to an enum in C#?

YourEnum foo = (YourEnum) Enum.Parse(typeof(YourEnum), yourStr);
if (!Enum.IsDefined(typeof(YourEnum), foo) && !foo.ToString().Contains(","))
{
    throw new InvalidOperationException($"{yourStr} is not an underlying value of the YourEnum enumeration.")
}

2020-03-17