0
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.

8
  • 1
    Enum.Parse will convert the string "Red" to the enum value. To convert the character 'R' to the enum value, just cast it: (Color)'R' Commented Feb 20, 2018 at 16:52
  • @BJMyers Of course that approach lacks error handling. Commented Feb 20, 2018 at 16:53
  • 3
    For a start "R" != 'R'. Then char enums aren't really practical. It may be worth to use const fields instead (see wpf Colors), though we don't know what is the purpose of Color enum which has values like Red = 'R'. Commented Feb 20, 2018 at 16:55
  • 1
    @MattD Enum.Parse does not take a char as a parameter. Commented Feb 20, 2018 at 16:58
  • 3
    Do you have a specific purpose for using those char values instead of int values? What are you trying to do? Commented Feb 20, 2018 at 17:00

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

"To convert the character R (i.e. the number 89)" I think you meant 82, the ascii code for R as in the Color enum you posted above
I did indeed. Thanks. :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.