【发布时间】:2018-01-08 01:36:41
【问题描述】:
var condition = false;
Foo($"String is {(condition ? "True" : "False")} works");
但是
Foo($"String is {(condition ? "True"
: "False")} fails");
当我在字符串插值语句中很好地格式化条件运算符时出现编译错误?
【问题讨论】:
-
使用逐字字符串.... Foo($@"String is {(condition ? "True" : "False")}fails");
-
感谢您的回复,但我使用 $@ 阅读了“解决方案”,这很奇怪,因为条件运算符已经需要用括号分隔,因此不应将其内容解释为不再使用格式说明符;使用 $@ 也无法使用格式文字,如 \t ...
标签: c# conditional-operator string-interpolation