【发布时间】: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