How to convert a string to enum in C#
To convert a string to enum, we can use the Enum.parse()
method in C#.
Here is an example:
using System;
class ConvertEnum {
enum Vehicle { Bus = 14, Lorry = 2, Car = 34 };
static void Main() {
Vehicle name= (Vehicle)Enum.Parse(typeof(Vehicle), "Bus");
System.Console.WriteLine(name);
}
}
Output:
Bus