why cant i create a property in class starting with a digit or special character?
P.S. I'm kinda new to c#
public class Test
{
public int 1property {get;set;}
}
If you're like me, and you were searching for this because you wanted to deserialize JSON from a third-party API that named properties starting with digits:
using Newtonsoft.Json;
public class ThirdPartyAPIResult
{
[JsonProperty("24h_volume_usd")]
public double DailyVolumeUSD { get; set; }
}
As per C# Identifier rules - Identifiers cannot start with a digit.
So you can not create variablename
,classname
,methodname
,interfacename
or propertyname
starting with a digit.
but Identifiers can start with underscore
.
Try this:
public class Test
{
public int _1property {get;set;}
}
Because the C# language specifications (specifically, section 2.4.2) state that you cannot.
It also makes things easier for the parser in terms of figuring out whether a given token is a literal number of an identifier. Identifiers can have numbers in them, they just can't start with a number. The first letter of any identifier must be a character or the underscore character (_
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With