-1

I've written a class definition like

 public class Item
        {
            public double? 30dhi { get; set; }
            public double? 30dlo { get; set; }
            public double? 7dhi { get; set; }
            public double? 7dlo { get; set; }
        }

Why it's not allowing me to add properties having alphanumeric keys The error is like below

invalid token '30d' for class , struct or interface member declaration

Mani
  • 2,391
  • 5
  • 37
  • 81
  • you can write `_30dhi` instead – M.kazem Akhgary Jul 13 '15 at 05:57
  • can someone explain me what is the point on downvotes ? – M.kazem Akhgary Jul 13 '15 at 05:59
  • Use textual names like MonthHigh or WeekLow, or not so good in my eyes Lo7d, Hi30d – holroy Jul 13 '15 at 05:59
  • @holroy: "Month" and "30 day" aren't the same thing though. I agree the current names aren't good, but I wouldn't change "30 day" to "month". – Jon Skeet Jul 13 '15 at 06:02
  • @M.kazemAkhgary: perhaps downvoters feel that a question the answer to which is in TFM, "does not show any research effort" (one of the explicit reasons stated for downvotes). – Peter Duniho Jul 13 '15 at 06:03
  • Note that the question of which this is a duplicate is specifically asking about class names, but the rules are the same as for property names; that question has an extensive discussion the rules for identifiers in C#, including at least two answers that are nearly identical in content to the one provided below. – Peter Duniho Jul 13 '15 at 06:05
  • @jonskeet, I know 30 days are not always a month, but in some contexts that is what it is used for... – holroy Jul 13 '15 at 06:05
  • @holroy: In *those* cases, I would agree - but why would you *assume* that the OP is using "30d" incorrectly, and suggest the change? – Jon Skeet Jul 13 '15 at 06:06
  • @jonskeet, I was merely indicating alternative names to avoid starting the names with numbers which your answer correctly states is illegal. Suggested MonthHigh to get OP thinking in a different way, but still credit him to choose proper names. And most, but not all, times when I see 7 and 30 the context often is week and month. – holroy Jul 13 '15 at 06:16
  • @holroy: Well, I think that was somewhat misleading - it would have been more useful IMO to think of what the OP should use if they really *do* mean 30 days. `ThirtyDayHigh`? (I have the opposite experience to you - we always *do* mean 30 days rather than 1 month...) – Jon Skeet Jul 13 '15 at 06:18
  • @jonskeet, point taken, will try to be clearer in the future – holroy Jul 13 '15 at 06:19

1 Answers1

9

Why it's not allowing me to add properties having alphanumeric keys

A property name has to be an identifier. Identifiers in C# can't start with a digit. You can have a digit after the first character, but not as the first character.

See section 2.4.2 of the C# 5 specification for the precise details of what is allowed for an identifier. The full grammar is too long to post here usefully, but the crucial part is:

identifier-or-keyword:
   identifier-start-character identifier-part-charactersopt

identifier-start-character:
  letter-character
  _ (the underscore character U+005F)

identifier-part-characters:
  identifier-part-character
  identifier-part-characters identifier-part-character

identifier-part-character:
  letter-character
  decimal-digit-character
  connecting-character
  combining-character
  formatting-character

letter-character:
  A Unicode character of classes Lu, Ll, Lt, Lm, Lo, or Nl
  A unicode-escape-sequence representing a character of classes Lu, Ll, Lt, Lm, Lo, or Nl

Note that the example of 30d is a great one for why it's not allowed - that's actually a numeric literal, of the value 30 and type double. (You've actually got 30dhi of course, but the parser has parsed 30d as a token.)

Community
  • 1
  • 1
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194