views:

240

answers:

5

Had a coworker ask me this, and in my brain befuddled state I didn't have an answer:

Why is it that you can do:

string ham = "ham " + 4;

But not:

string ham = 4;

If there's an implicit cast/operation for string conversion when you are concatenating, why not the same when assigning it as a string? (Without doing some operator overloading, of course)

A: 

The value of the righthand side of the first expression is a string, while the value of the righthand side of the second expression is not. The concatonation is providing the magic in the first scenario, where the assignment isn't doing anything special. In the second scenario, the assignment continues to play dumb.

Mark Canlas
+13  A: 

When concatenating the compiler turns the statement "ham" + 4 into a call to String.Concat, which takes two object parameters, so the value 4 is boxed and then ToString is called on that.

For the assignment there is no implicit conversion from int to string, and thus you cannot assign 4 to a string without explicitly converting it.

In other words the two assignments are handled very differently by the compiler, despite the fact that they look very similar in C#.

Brian Rasmussen
Effectively true, but I don't know that it actually calls the Concat method or generates equivalent code. The reference seems to indicate the latter.
tvanfosson
If you look at the release mode code with Reflector (set it to display IL), you see that it does call Concat.
Brian Rasmussen
+2  A: 

There is no implicit conversion when doing concatenation. String concatenation resolves down to a String.Concat call, which has an overload which takes Objects. It is this overload which performs an (explicit) conversion to string.

itowlson
A: 

The expression

"ham " + 4

Forces an implicit conversion of 4 to a string based on the combination of a string type and the addition operator. Specifically it's a quality of the "+" operator, and when doing operator overloading you can manually implement the same type of thing.

A similar and less obvious example would be:

long myNumber = Int64.MaxValue - 1;

In this case "1" should be evaluated as a 32-bit integer but it is implicitly converted. You can check the C# language spec section 6.1 for an exhaustive list of implicit conversions supported by the compiler.

edit: to be clear, the language spec section i referred to lists implicit conversions supported by the compiler, while operators like "+" can have their own supported conversions.

Barry Fandango
There is no implicit conversion of int to string.
jalf
That's correct. There is an implicit conversion tied to the "+" operator.
Barry Fandango
The + operator isn't defined for String. The compiler generates corresponding calls to Concat instead.
Brian Rasmussen
I stand corrected, thanks Brian.
Barry Fandango
+4  A: 

Binary + operators are predefined for numeric and string types. For numeric types, + computes the sum of its two operands. When one or both operands are of type string, + concatenates the string representations of the operands.

Reference

The assignment operator (=) stores the value of its right-hand operand in the storage location, property, or indexer denoted by its left-hand operand and returns the value as its result. The operands must be of the same type (or the right-hand operand must be implicitly convertible to the type of the left-hand operand).

Reference

tvanfosson