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.
[C#]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.
[C#]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
- C# Reverse Bytes (Little/Big Endian) – how to reverse byte order
- Enum.ToString – MSDN – converts enum to string
- Enum.Parse – MSDN – converts string to enum