【问题标题】:String interpolation: a bug in c# compiler?字符串插值:c# 编译器中的错误?
【发布时间】:2019-05-10 12:05:15
【问题描述】:
string Test(bool @bool) => $"you're {@bool?"hired":"fired"} Have a nice day!";
上面的代码导致编译错误。
但为什么?
请注意
string test = $"this {"is"} working";
有效。
【问题讨论】:
标签:
c#
string
interpolation
compiler-bug
【解决方案1】:
冒号结束插值。只需将条件括起来:
string Test(bool @bool) => $"you're {(@bool ? "hired":"fired")} Have a nice day!";
【解决方案2】:
对于这个问题,你不能使用?,: 之类的东西,使用这些你必须设置你的条件应该放在() 中,比如:
string Test(bool @bool) => $"you're {(@bool ? "hired":"fired")} Have a nice day!";
【解决方案3】:
您可以尝试使用() 包含您的?: 运算符
string Test(bool @bool) => $"you're {(@bool ? "hired":"fired")} Have a nice day!";
$ - string interpolation