String To Enum Conversion [C#]

This example shows how to convert enum values to string and reversely.

Enum to string

To convert enum to string use simply Enum.ToString method.

Animal animal = Animal.Cat;
string str = animal.ToString();  // "Cat"

String to enum

To convert string to enum use static method Enum.Parse. Parameters of this method are enum type, the string value and optionally indicator to ignore case.

string str = "Dog";
Animal animal = (Animal)Enum.Parse(typeof(Animal), str);  // Animal.Dog
Animal animal = (Animal)Enum.Parse(typeof(Animal), str, true); // case insensitive

See also

by Texy2! -->;