【问题标题】:C#: ternary expressions inside interpolation are invalid [duplicate]C#:插值内的三元表达式无效[重复]
【发布时间】:2018-02-12 23:46:55
【问题描述】:

我想在插值中插入一个三元表达式,如下所示:

var a = $"we have {true ? "a":"b"}";

编译使用的软件:

XBuild Engine Version 14.0
Mono, Version 5.2.0.215

我使用了xbuild Cmc.sln(Cmc 是我的解决方案名称)。之后我得到了错误(第 44 行是上面给出的 C# 代码):

Expr/Expression.cs(44,13): error CS8076: Missing close delimiter '}' for interpolated expression started with '{'.
Expr/Expression.cs(44,25): error CS1003: Syntax error, ':' expected
Expr/Expression.cs(44,25): error CS1733: Expected expression
Expr/Expression.cs(44,27): error CS1002: ; expected
Expr/Expression.cs(44,28): error CS1002: ; expected

为什么我会得到这个?我认为我的代码是有效的。

【问题讨论】:

  • 是的,它是重复的...但是我搜索了C# tenary,但没有得到答案。所以我提出了一个新问题......对不起。

标签: c# mono interpolation


【解决方案1】:

试试:

$"we have {(true ? "a":"b")}";

【讨论】:

猜你喜欢
  • 2019-03-09
  • 2014-12-25
  • 1970-01-01
  • 2017-07-09
  • 2016-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-02
相关资源
最近更新 更多