1

I have the following Class definition:

class PostObject
{
    public string jsonrpc { get; set; }
    public string method { get; set; }
    public MyObject params { get; set; }
    public string id { get; set; }
}

I use this class for a post call (serialized in json) and the server has 'params' as an input post var and there's no way to change it.

The question is that as params is a reserved keyword in c#, what should I do?

Apalabrados
  • 1,038
  • 8
  • 20
  • 35

3 Answers3

5

You can use serialization attributes to set it's name. Like this (NewtonSoft.Json):

[JsonProperty(PropertyName = "params")]
public MyObject parameters { get; set; }

Serialization Attributes in NewtonSoft.Json

Danil Eroshenko
  • 461
  • 2
  • 9
  • This is definitely the better way to do it, so you can use "C# style" property names, just didnt want to assume (in my comment) that OP was using newtonsoft! – Chris Watts Sep 07 '17 at 10:19
4

Please see MSDN for the same. You can still use keywords as variable name but by appending '@'. Example :- int @int= 100;

Source,

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/

Vijayanath Viswanathan
  • 7,170
  • 3
  • 22
  • 41
1

Other than the @ escape and JsonPropertyAttribute, it is also worth mentioning that you don't need to follow the exact capitalization with a library like NewtonSoft.Json:

public string Params { get; set; }
Xiaoy312
  • 13,904
  • 1
  • 29
  • 40