Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add `+` to a property name in C#?

Tags:

json

c#

How do I declare a property or variable name as fg+ in C#?

public string fg+ { get; set; } 

I am getting fg+ as a field in a response from JSON as "fg+": "103308076644479658279".

I am deserializing it using a class. How do I read it from JSON?

like image 521
Musakkhir Sayyed Avatar asked Mar 15 '16 10:03

Musakkhir Sayyed


People also ask

How do you declare a property in C#?

A property may be declared as a static property by using the static keyword or may be marked as a virtual property by using the virtual keyword. Get Accessor: It specifies that the value of a field can access publicly. It returns a single value and it specifies the read-only property.

What are accessors in C#?

A get property accessor is used to return the property value, and a set property accessor is used to assign a new value. In C# 9 and later, an init property accessor is used to assign a new value only during object construction. These accessors can have different access levels.

What are properties in C sharp?

Property in C# is a member of a class that provides a flexible mechanism for classes to expose private fields. Internally, C# properties are special methods called accessors. A C# property have two accessors, get property accessor and set property accessor.

What is the difference between field and property in C#?

Fields are ordinary member variables or member instances of a class. Properties are an abstraction to get and set their values. Properties are also called accessors because they offer a way to change and retrieve a field if you expose a field in the class as private.


1 Answers

Such a variable name isn't legal in C#.

If you're using Json.NET to deserialize your data, you can use the JsonProperty attribute to give it a custom name:

[JsonProperty("fg+")] public string Fg { get; set; } 
like image 62
Yuval Itzchakov Avatar answered Sep 19 '22 04:09

Yuval Itzchakov