14

Is thre any way to have an enum entry with a hyphen, "-", in the name, for example:

enum myEnum
{
   ok,
   not-ok,
}

I've seen the question about enums having friendly names however it would save me a bit of work if I can use a hyphen directly.

Update: The reason I want to use a hyphen is it makes it easy to use an enum for lists of set values which I have no control over such as:

 rejected
 replaced
 local-bye
 remote-bye
Community
  • 1
  • 1
sipsorcery
  • 30,273
  • 24
  • 104
  • 155
  • 7
    If you are trying to split words out of the Enum value, try using CamelCase – benPearce Feb 24 '10 at 04:21
  • -1 Programming languages are not by means a human language, and therefore may not understand every variation we are capable of. In any programming language I have worked with, I have never ever seen hyphens, except for the quoted identifier in TSQL or PLSQL, which allows mostly for convenient column naming convention as per the result set. CamelCase is the far netter approach. – Will Marcouiller Feb 25 '10 at 14:23
  • possible duplicate of [Can my enums have friendly names?](http://stackoverflow.com/questions/1415140/can-my-enums-have-friendly-names) – nawfal Jun 08 '13 at 23:40

4 Answers4

23

No, a hyphen is not allowed.

Identifiers

You could obviously replace the hyphen with an underscore, but as @benPearce suggested, CamelCase would be a better choice, and in line with most C# coding standards.

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
  • 1
    I'd actually just been looking over that page. I don't knwo why they couldn't just put _ instead of Pc and a reference to the unicode doc. – sipsorcery Feb 24 '10 at 04:32
  • I still can't find the official definition of "connector punctuations" – Greg Feb 24 '10 at 16:59
17

Suppose you have:

enum E { a = 100 , a-b = 200 };
...

E b = E.a;
int c = (int)(E.a-b);

Is c set to 200, or 0 ?

Allowing hyphens in identifiers would make the language almost impossible to analyze lexically. This language is already hard enough to analyze what with << and >> each having two completely different meanings; we don't want to make it harder on ourselves.

The naming guidelines say to use CamelCasing for enum values; follow the guidelines.

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
  • I'd happily follow the guidelines but the string list the enum is needed to represent is out of my control. Lexical analysis may be hard getting half a dozen RFC's to remove hyphenated words is not easy either. – sipsorcery Feb 24 '10 at 19:53
5

In case somebody needs to use hyphens I think this approach can help: Use underscore in your enum declaration and then replace the underscore with hyphen.

enum myEnum { ok, not_ok, }

var test = myEnum.not_ok;
// let's say this value is coming from database or somewhere else
var someValue = "not-ok"; 

if(test.ToString().Replace("_","-").equals(someValue)) { /* some stuff */ }

Not the best practice, but can help if you don't have control over "someValue" and need to use enums.

Miguel
  • 1,575
  • 1
  • 27
  • 31
  • I did something like: Status = sqlDataReader.GetString(5).Equals("NotChecked") ? "Not Checked" : "Checked", and yeah it works (since my Enum only has two items) – paraJdox1 Jun 15 '21 at 07:23
0

You say "I'd happily follow the guidelines but the string list the enum is needed to represent is out of my control". Since this is why you want hyphens, I suggest you override the toString() method of the enums you need this for, and call toString() in your code where you need to represent the names.

  • Possible, but attributes make more sense (see the link in the question). Overriding ToString() just to get friendly names is not reasonable. – Nikolai Samteladze Oct 19 '12 at 22:24