convertion of enum values to string and reversely

Posted by Prabhukiran345 under C# category on | Points: 40 | Views : 823
To convert enum to string it is best option to use Enum.ToString method

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

To convert string to enum it is best option to use static method Enum.Parse .
string str = "Dog";
Animal animal = (Animal)Enum.Parse(typeof(Animal), str); // Animal.Dog
Animal animal = (Animal)Enum.Parse(typeof(Animal), str, true); // case insensitive

Comments or Responses

Login to post response