2017-09-04 45 views
0

我想插入一个tenary表达成插值,是这样的:在编译使用C#:内插值tenary表达式是无效

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 

为什么会这样?我认为我的代码是有效的。

+0

是啊,这是重复的...但我搜索'C#tenary',我没有得到答案。所以我开了一个新的问题...对不起。 – ice1000

回答

1

尝试:

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

哇谢谢你!但为什么?你知道我没有括号的错误吗? – ice1000

+0

在https://stackoverflow.com/questions/31844058/how-to-use-the-ternary-operator-inside-an-interpolated-string中的解释应用 –

+0

是的,我刚才看到,但无论如何谢谢。 – ice1000