For converting a string to an enum, which of the following ways is better?
-
This code:
colorEnum color = (colorEnum)Enum.Parse(typeof(colorEnum), "Green"); -
or this:
string colorString = ... colorEnum color; switch (colorString) { case "Green": color = colorEnum.Green; break; case "Red": color = colorEnum.Red; break; case "Orange": color = colorEnum.Orange; break; .... }
You should use the Enum.TryParse, if it fails you can handle the error correctly.
sample: