Updated on Kisan Patel
This tutorial will show you how to get the enum value by string or int in C#?
For example,
public enum Fruit { Banana = 1, Apple = 2, Grapes = 3 }
Now, to get enum value from a string then you need to follow below approach:
Fruit foo = (Fruit) Enum.Parse(typeof(Fruit), "Apple"); Console.WriteLine(foo.ToString()); //print "Apple" Console.WriteLine((int)foo); //print 2
If you want to get enum value from a int then you need to follow below approach:
Fruit foo = (Fruit)3; Console.WriteLine(foo.ToString()); //print "Grapes"
OR
Fruit foo = (Fruit) Enum.ToObject(typeof(Fruit), 3); Console.WriteLine(foo.ToString()); //print "Grapes" Console.WriteLine((int)foo); //print 3