Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a variable name as a type

Tags:

c#

json.net

I am deserializing a json object from a third party API. One of the Objects properties is a variable called "checked". In c#.net 'checked' is a type. Is there anyway to overcome this.

Im using Newtonsoft Json.net to do the deserializing.

Nothing special about the class,

public class item{
  public bool checked;
} 
like image 510
Malcor Avatar asked Feb 23 '26 17:02

Malcor


1 Answers

Use the @ to prefix the name:

public class item{
  public bool @checked;
}

By the way checked is not a type but a keyword.

From MSDN:

Keywords are predefined, reserved identifiers that have special meanings to the compiler. They cannot be used as identifiers in your program unless they include @ as a prefix.

like image 148
nemesv Avatar answered Feb 25 '26 07:02

nemesv



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!