How to use the ternary operator inside an interpolated string?
问题 I\'m confused as to why this code won\'t compile: var result = $\"{fieldName}{isDescending ? \" desc\" : string.Empty}\"; If I split it up, it works fine: var desc = isDescending ? \" desc\" : string.Empty; var result = $\"{fieldName}{desc}\"; 回答1: According to the documentation: The structure of an interpolated string is as follows: { <interpolationExpression>[,<alignment>][:<formatString>] } The problem is that the colon is used to denote formatting, like: Console.WriteLine($"The current