【问题标题】: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

      【讨论】:

        猜你喜欢
        • 2017-06-05
        • 1970-01-01
        • 2016-09-17
        • 1970-01-01
        • 2022-03-24
        • 1970-01-01
        • 2016-12-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多