Vlasta
Vlasta

Reputation: 31

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.

Upvotes: 3

Views: 933

Answers (3)

Jeroen Vannevel
Jeroen Vannevel

Reputation: 44449

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

[JsonProperty(PropertyName = "return")]
public string MyPropertyName {get; set;}

Upvotes: 4

MaMazav
MaMazav

Reputation: 1885

Try prefixing the variable name with '@'. http://msdn.microsoft.com/en-us/library/aa664670%28v=vs.71%29.aspx

Upvotes: 1

Joey
Joey

Reputation: 354576

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.

Upvotes: 3

Related Questions