Create a property starting with a digit

Why can't I create a property in a class starting with a digit or special character?

PS I'm new to C #

public class Test { public int 1property {get;set;} } 
+7
c # class
source share
4 answers

Because C # language specifications (specifically section 2.4.2) indicate that you cannot.

It also simplifies the parser in terms of figuring out whether a given token is an alphabetic identifier number. Identifiers can have numbers in them, they simply cannot begin with a number. The first letter of any identifier must be a character or underscore ( _ ).

+9
source share

If you are like me, and you were looking for this because you wanted to deserialize JSON from a third-party API, which named properties starting with numbers:

 using Newtonsoft.Json; public class ThirdPartyAPIResult { [JsonProperty("24h_volume_usd")] public double DailyVolumeUSD { get; set; } } 
+10
source share

Identifier / Property Names cannot begin with integer values. Consider the following alternatives:

 public class Test { public int PropertyOne {get;set;} public int Property1 {get;set;} public int Property_1 {get;set;} } 
+7
source share

In accordance with the rules of the identifier C # - Identifiers cannot begin with a digit.

Thus, you cannot create variablename , classname , methodname , interfacename or propertyname starting with a digit.

but identifiers may begin with underscore .

Try the following:

 public class Test { public int _1property {get;set;} } 
+5
source share

All Articles