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}";
.netc#-6.0c++string-interpolationternary-operator
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}";
Best Answer
According to the documentation:
The problem is that the colon is used to denote formatting, like:
The solution is to wrap the conditional in parenthesis: