2017-08-01 39 views
-1
var condition = false; 
Foo($"String is {(condition ? "True" : "False")} works"); 

多线条件运算符在C#中失败

Foo($"String is {(condition ? "True" 
          : "False")} fails"); 

我编译错误,当我良好的格式化插值声明我的字符串中的条件运算符?

+0

使用逐字字符串....美孚($ @ “字符串是{(条件? ”真“ : ”假“)}失败”); –

+0

感谢您的回复,但我通过使用$ @阅读了'解决方案',这很奇怪,因为条件运算符已经需要用括号分隔,因此它的内容不应该被解释为格式说明符了;使用$ @也不可能使用像\ t这样的格式文字... –

回答

0

使用@

Foo([email protected]"String is {(condition ? "True" 
          : "False")} fails"); 
+0

但是我怎样才能使用像\ t那样的字符串文字呢? –

+0

你不能。你必须选择一个'\ t'或'@' – Marusyk