Search code examples
c#anonymous-types

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


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.


Solution

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

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