2

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,098
  • 8
  • 21
  • 38

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
  • 470
  • 2
  • 10
  • 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
5

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

Source,

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

Vijayanath Viswanathan
  • 8,027
  • 3
  • 25
  • 43
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
  • 14,292
  • 1
  • 32
  • 44