by Sai gowtham

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();
}

Top Udemy Courses

JavaScript - The Complete Guide 2020 (Beginner + Advanced)
JavaScript - The Complete Guide 2020 (Beginner + Advanced)
45,614 students enrolled
52 hours of video content
View Course
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
284,472 students enrolled
40 hours of video content
View Course
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
130,921 students enrolled
48.5 hours of video content
View Course