Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prohibited variable names

Is it somehow possible to call a C# variable return?

I need to deserialize JSON data and there is a field called return.

And I am unable to create class with this name to create object for deserialization :-/

Thank you.

like image 917
Vlasta Avatar asked May 08 '26 02:05

Vlasta


2 Answers

Either use @return or simply annotate it (JSON.NET):

[JsonProperty(PropertyName = "return")]
public string MyPropertyName {get; set;}
like image 171
Jeroen Vannevel Avatar answered May 09 '26 16:05

Jeroen Vannevel


You can use keywords as identifiers by prefixing them with @:

public int Foo()
{
    int @return = 5;
    return @return;
}

Note that this is not necessary for the so-called contextual keywords, such as LINQ operators, var and others that came later. Those have special rules where they can appear which allow them to be used as identifiers.

like image 22
Joey Avatar answered May 09 '26 16:05

Joey