public enum Color { Red = 'R', Blue = 'B', Green = 'G', Yellow = 'Y', } Color color = (Color)Enum.Parse(typeof(Color), "R"); I tried to parse an string value with an char enum, but it throws me an System.ArgumentException.
The first thing to understand is that C# does not support char as an underlying type for enumerations. It is illegal to declare an enum like this:
public enum Color : char The underlying type of your Color enum is actually int, since no other type is specified in its declaration.
C# will, however, allow you to assign a character literal as the value of an enum member - by converting it to the corresponding ASCII integer. So your Color enum declaration is identical to this:
public enum Color { Red = 82, Blue = 66, Green = 71, Yellow = 89, } If you think of the enumeration in terms of integral values, the problem becomes a bit clearer. There is no relationship between the string "R" and Color.Red. There is a relationship between the character 'R' and Color.Red, but only because the character is (implicitly) converted to its ASCII representation.
Enum.Parse is designed to convert a string to an enumeration based on the name of the declared enum member, not its value. Therefore, Enum.Parse will only work if you want to convert "Red" to Color.Red. To convert the character R (i.e. the number 82) to Color.Red, you simply cast it to the enum type:
Color red = (Color)'R'; If your goal is to convert the string "R" to Color.Red, you would need to convert it to a char by treating the string as a character array:
Color red = (Color)"R"[0]; Methods such as Enum.IsDefined could be used beforehand to determined whether the input is a valid enum value. But as noted in comments above, this isn't really how enums are designed to be used. A char-ish enum may be the wrong tool for the job.
Enum.Parsewill convert the string "Red" to the enum value. To convert the character 'R' to the enum value, just cast it:(Color)'R'"R" != 'R'. Then char enums aren't really practical. It may be worth to useconstfields instead (see wpf Colors), though we don't know what is the purpose ofColorenum which has values likeRed = 'R'.charvalues instead ofintvalues? What are you trying to do?