c# int to enum 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: enumeratio nc sharp
enum Products
{
Apple = 0,
Milk = 1,
Vodka = 2,
}
Console.Writeline(Products.Milk);
Console.Writeline((int)Products.Milk);
Example 3: c# enum to int
Just cast the enum, e.g.
int something = (int) WeaponType.BFG;
Example 4: convert int to enum c#
YourEnum foo = (YourEnum)yourInt;