Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I declare a variable with hyphen on it

Tags:

c#

.net

Recently I was facing with a json object name declaration inside class and was able to resolve it with following:

  [JsonProperty("flv-270p")]
        public string flv270p { get; set; }

So If I want to declare flv-270p without JsonProperty how shall I do it?

like :

public string flv270-p { get; set; }
like image 430
Simsons Avatar asked Dec 04 '22 02:12

Simsons


1 Answers

Short answer: you can't.

.NET supports arbitrary member names, including those illegal in CIL languages. However if a language does not support it then you cannot use it, and this includes hyphens and dashes. Auto-properties in C# use < and > in their hidden backing field names for this reason.

C# does let you prefix keywords with the @ symbol to allow you to use it as a variable or member name (e.g. public int @class or public void @struct(int @interface) but this is for the benefit of consumers.

However c# does not let you use hyphens in names under any circumstances because that character is used for the unary negation and binary subtraction sub operator. A name flv270-p is interpreted as "flv270 minus p".

like image 145
Dai Avatar answered Dec 20 '22 17:12

Dai