I’m looking at some code golf in LINQPad and wondering why:
int c;
string o;
o+=c;//this works
o+=P==2?"."+c:c;//this doesn't
o+=P==2?"."+c:""+c;//this does
mostly why the first one works and the second one throws a “no implicit conversion between ‘string’ and ‘int'” error.
Your second, non-working example has inconsistent types in the
?:operator. What you have is:(The types in parentheses above are there to clarify what the existing types are, not to cast them to a different type.)
Both sides of the
:in the ternary operator must be of the same type. For this reason, your second example is a syntax error. The third example works, because concatenating with an empty string coercescinto a string.