inconsistent behavior with string+=int in c#
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.
The +
operator on strings can accept an int, resulting in another string. However, there is no implicit (or explicit) cast from int to string.
When you use the ternary operator ?:
, both "branches" need to either be the same type, or the type of one has to be implicitly convertible to the other.
In your second example, the first branch is a string, after the +
operator is done, but the second is just an int, so it doesn't work. In your third example, both branches are strings, so it's fine.
Your second, non-working example has inconsistent types in the ?:
operator. What you have is:
o += (P == 2 ? (string) "." + c : (int) c);
(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 coerces c
into a string.
The +=
operator uses the +
operator, so the first is really:
o = o + c;
What the compiler actually creates is:
o = String.Concat((object)o, (object)c);
The integer is boxed, and the Concat
method that takes object
parameters is called. The ToString
method will be called on both parameters to get their string values, and they are concatenated.
If you first convert the integer to a string, the code gets more straight forward:
o += c.ToString();
which becomes:
o = String.Concat(o, c.ToString());
In the second code, the types in the conditional operator doesn't match:
bool ? string : int
The second and third operand has to have the same type, like in the third code:
bool ? string : string
The second code really becomes:
o = String.Concat(
o,
P == 2
? String.Concat((object)".", (object)c)
: c
);
and the third code really becomes:
o = String.Concat(
o,
P == 2
? String.Concat((object)".", (object)c)
: String.Concat((object)String.Empty, (object)c)
);
Anyhow, you should consider using a StringBuilder
to build strings instead of using the +=
operator:
StringBuilder builder = new StringBuilder;
if (P == 2) {
builder.Append('.');
}
builder.Append(c);
精彩评论