将意外令牌放入三元运算符
Getting unexpected token into ternary operator
我想做一个简单的三元运算,例如:
progressToBackCheckMedianString = $"{newLine} Medians {(medianInProgressFormattedTime != string.Empty ? {newLine} {medianInProgressFormattedTime}{newLine} : string.Empty)}" ;
但我得到
Unexpected token '{'
和 {(medianInProgressFormattedTime != string.Empty ?
被标记为红色并带有此错误。我做错了什么?此致
在{newLine} {medianInProgressFormattedTime}{newLine}
周围添加字符串引号
progressToBackCheckMedianString = $"{newLine} Medians {(medianInProgressFormattedTime != string.Empty ? $"{newLine} {medianInProgressFormattedTime}{newLine}" : string.Empty)}";
您正在使用$ - string interpolation,支持高于6.0的c#版本
{interpolatedExpression}
大括号在语法中有特殊含义。
您的 newLine
看起来像一个字符串值。
去掉newLine
之间的{
和}
,用+
连接字符串值,因为外面已经用大括号了
我会用
string.IsNullOrEmpty
检查字符串值而不是
medianInProgressFormattedTime != string.Empty
因为 medianInProgressFormattedTime
可能是 NULL
。
string progressToBackCheckMedianString = $"{newLine} Medians{(!string.IsNullOrEmpty(medianInProgressFormattedTime) ? newLine + medianInProgressFormattedTime + newLine : string.Empty)}";
我想做一个简单的三元运算,例如:
progressToBackCheckMedianString = $"{newLine} Medians {(medianInProgressFormattedTime != string.Empty ? {newLine} {medianInProgressFormattedTime}{newLine} : string.Empty)}" ;
但我得到
Unexpected token '{'
和 {(medianInProgressFormattedTime != string.Empty ?
被标记为红色并带有此错误。我做错了什么?此致
在{newLine} {medianInProgressFormattedTime}{newLine}
progressToBackCheckMedianString = $"{newLine} Medians {(medianInProgressFormattedTime != string.Empty ? $"{newLine} {medianInProgressFormattedTime}{newLine}" : string.Empty)}";
您正在使用$ - string interpolation,支持高于6.0的c#版本
{interpolatedExpression}
大括号在语法中有特殊含义。
您的 newLine
看起来像一个字符串值。
去掉newLine
之间的{
和}
,用+
连接字符串值,因为外面已经用大括号了
我会用
string.IsNullOrEmpty
检查字符串值而不是
medianInProgressFormattedTime != string.Empty
因为 medianInProgressFormattedTime
可能是 NULL
。
string progressToBackCheckMedianString = $"{newLine} Medians{(!string.IsNullOrEmpty(medianInProgressFormattedTime) ? newLine + medianInProgressFormattedTime + newLine : string.Empty)}";