How can i get enum to contain a dash (-)?
You can't. Full stop. However, there are workarounds. You can, e.g., use DescriptionAttribute
:
public enum PackageMedium : int {
NTP,
DAT,
Exabyte,
[Description("CD-ROM")]
CDROM,
DLT,
D1,
DVD,
BD,
LTO,
LTO2,
LTO4
}
This means, unfortunately, that you have more work to do when mapping values. On the other hand, it at lest compiles.
If you don't like that, pick another workaround, e.g., a dictionary:
var dict = Enum.GetValues(typeof(PackageMedium))
.Cast<PackageMedium>()
.Select(v => Tuple.Create(v == PackageMedium.CDROM ? "CD-ROM" : v.ToString(), v))
.ToDictionary(t => t.Item1, t => t.Item2);
var myEnumVal = dict["CD-ROM"];
I know this is late, but this can be usefull.
Another workaround is to use the XmlEnumAttribute :
public enum PackageMedium : int {
NTP,
DAT,
Exabyte,
[XmlEnumAttribute("CD-ROM")]
CDROM,
DLT,
D1,
DVD,
BD,
LTO,
LTO2,
LTO4
}
It is used to validate an xml with an xsd. No additional code needed.
Short answer: No.
The reason being is that the -
character is used as a token by the lexer for other purposes such as representing the binary and unary minus operator.
Your best bet is to either remove the -
or replace it with some other character that is a valid character in identifier names. The only non-letter character you can generally use is _
.
You can find more information in the C# Specification.