How to convert string to ENUM value ?( .NET interview questions with answers)
- By Shiv Prasad Koirala in .Net
- Feb 5th, 2014
Note this question can also asked in a different format "Explain Enum.Parse() function ?".
Many times we would like to convert string in to ENUM objects. For example let's say you have a COLOR Enum and you would like to pass string "Red" and get ENUM object "color.Red".
"ENUM.Parse" method parses string to an ENUM value. Like in the below code we pass "Red" string value and it gets type casted to enum which has green enum as a constant.
MyColors EnumColors = (MyColors)Enum.Parse(typeof(MyColors), "Red");
You can see the below video created by www.questpond.com team which demonstrates how string can be converted to a ENUM constant.
Shiv Prasad Koirala
Visit us @ www.questpond.com or call us at 022-66752917... read more