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
1

Leave a Comment