c# Anonymous type. How to use datatype name as member name

150 Views Asked by At

I have to create a json query dynamically where one of the properties is called "bool". I need this name because the system I send the requests to, expects this naming.

To create the json I use C# anonymous types like:

var myquery = new { bool = "Yes" };

but I'm not allowed to use bool as member name. Is there a fix for that somehow?

I have searched for a solution, without any success. I hope there is an easy fix.

2

There are 2 best solutions below

2
Magnus On BEST ANSWER

Yes, you put the @ character in front of the variable.

var myquery = new { @bool = "Yes" };
0
L01NL On

You can prefix it with an @.

var myquery = new { @bool = "Yes" };