开发者

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

开发者 https://www.devze.com 2022-12-07 17:40 出处:网络
This question already has an answer here: How do I use a C# keyword as a property name? (1 answer) Closed 1 hour ago.
This question already has an answer here: How do I use a C# keyword as a property name? (1 answer) Closed 1 hour ago.

I have to create a json query dynamically where one of the properties is called "bool". I n开发者_StackOverflow中文版eed 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.


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

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


You can prefix it with an @.

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

精彩评论

暂无评论...
验证码 换一张
取 消