by @saigowthamr

How to cast int to enum in c#

Home » c sharp » How to cast int to enum in c#

The below example will show how to cast an int to enum in c#.

public enum MyEnum : int
{
    Apple = 1,
    Grapes = 2,
    Bananna = 3
}

static void Main(string[] args)
{
    var e1 = (MyEnum)1; // Apple
    var e2 = (MyEnum)2; // Grapes

    Console.WriteLine("{0} {1}", e1, e2);
    Console.ReadLine();
}

Best Web Development courses