How can I use a special char in a C# enum?
Even if you could do that (and it looks you can't), it probably wouldn't be a good idea, because you'd be mixing how the enum should be displayed with the program code to manipulate it. A better option would be to define an attribute (or use existing DisplayNameAttribute
) and annotate your enum with names as additional meta-data:
public enum Unit{
[DisplayName("Hz")] Hertz,
[DisplayName("%V")] Volt
}
Enum members shouldn't be used for user interface display purposes. They should be mapped to a string in order to get displayed. You can create a string array (or a dictionary) that maps each enum member to a string for user interaction.
That said, to answer your question directly, you can use As Henk points out, this won't work for \uxxxxV
were xxxx
is the hexadecimal number representing the Unicode code point for %
. This is far from recommended.%
as it's not in Unicode classes Lu, Ll, Lt, Lm, Lo, Nl, Mn, Mc, Nd, Pc, Cf (letters, digits, connecting, and formatting characters). Only these characters are acceptable for identifiers.
Just to register another way to do that, in a simple way, you can define "your own" enumerator with constants. In your example
public class UnitEnum
{
public const string KW = "KW";
public const string Volt = "%V";
}
To access, it just: UnitEnum.Volt