Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# Using params variable name in Class

Tags:

c#

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?

like image 806
Apalabrados Avatar asked Feb 02 '26 12:02

Apalabrados


2 Answers

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

like image 143
Danil Eroshenko Avatar answered Feb 04 '26 01:02

Danil Eroshenko


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/

like image 42
Vijayanath Viswanathan Avatar answered Feb 04 '26 00:02

Vijayanath Viswanathan