【问题标题】:C# String Interpolation and the Conditional OperatorC# 字符串插值和条件运算符
【发布时间】:2020-10-06 02:29:49
【问题描述】:

如果我的 {condition} 为假,我如何在字符串中返回 null。我正在使用带有条件运算符的字符串插值。

string returnURL = $"{ condition ? (ConfigurationManager.AppSettings["ApplicationURL"]}/my-page/{trip?.TripUId }) : null"

【问题讨论】:

  • 加括号:$"{(condition ? ... : ...)}"
  • 我不清楚你想要达到什么目的。如果condition 为假,您是否希望returnURL 本身为空?带有示例输入和输出的minimal reproducible example 会更容易为您提供帮助。
  • 发送错误信息。
  • (我怀疑你应该只使用条件运算符 outside 插值字符串文字:string returnUrl = condition ? $"{ConfigurationManager.AppSettings["ApplicationURL"]}/my-page{trip?.TripUid}" : null;

标签: c# conditional-statements string-interpolation


【解决方案1】:

这样使用

bool flag = true;
string returnUrl = flag ? $"{ConfigurationManager.AppSettings["ApplicationURL"]}/my-page{trip?.TripUid}" : null;

【讨论】:

    猜你喜欢
    • 2015-02-14
    • 2016-02-02
    • 2020-11-22
    • 1970-01-01
    • 2011-03-11
    • 2018-01-08
    • 2016-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多