Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create a property starting with digit

Tags:

c#

.net

class

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;}
}
like image 668
user3108411 Avatar asked Dec 23 '13 17:12

user3108411


3 Answers

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; }
}
like image 116
DrShaffopolis Avatar answered Sep 21 '22 12:09

DrShaffopolis


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;}
}
like image 30
Sudhakar Tillapudi Avatar answered Sep 21 '22 12:09

Sudhakar Tillapudi


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 (_).

like image 25
Servy Avatar answered Sep 19 '22 12:09

Servy