3

This is super dumb, but I've googled and checked references and I just cannot find an answer... Why can an int or float etc be added as part of a string without converstion but not on it's own? that is:

while this work fine:

 string st = "" + 12;

this doesn't (of course):

 string st = 12;

Where is the magic here? I know it works I just want to know WHY it works and how I control HOW the conversion is done?

Anthony Pegram
  • 123,721
  • 27
  • 225
  • 246
  • 4
    Check this link: http://stackoverflow.com/questions/4472900/how-is-null-true-a-string – Chandu Dec 20 '10 at 14:06
  • 1
    @Cybernate: I **knew** someone was going to link that :) – BoltClock Dec 20 '10 at 14:08
  • :).. Yep.. Its on my favorites list.. – Chandu Dec 20 '10 at 14:10
  • See the answer by `Jon Skeet` accepted by me on http://stackoverflow.com/questions/4472900/how-is-null-true-a-string – Javed Akram Dec 20 '10 at 14:18
  • This was alot more complicated then I expected but I now fully understand the reasons behind this (strange) behavior. I guess that you could call the + operator behavior of strings as "implicit conversion with an explicit feel" which was what amazed me the most, it seemed so explicit. – Kristian Erik Tigersjäl Dec 20 '10 at 14:57

7 Answers7

4

In the first statement, the left operand for the + is a string, and as such + becomes the concatenation operator. The compiler finds the overload for that operator which takes a string and an arbitrary value as operands. This converts both operands to strings (using ToString()) then joins them.

The second statement does not work because there's no implicit cast from int to string.

You can control how the conversion is done by using parentheses to change the order of operations (semi-effective) or by writing code to handle the conversions pre-emptively.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
1

This is how string concatenation is designed to work, and as BoltClock's answer noted, the compiler is using the + as the string concatenation operator. From the C# Language Specification on string concatenation, we find that:

Any non-string argument is converted to its string representation by invoking the virtual ToString method inherited from type object.

Community
  • 1
  • 1
Donut
  • 110,061
  • 20
  • 134
  • 146
1

String concats in .NET ultimately resolve to calls to one of the overloads of the static String.Concat methods. This is an optimization to reduce the number of temporary strings that would otherwise be created when mutliple concatenations occur in a single statement.

In short the reason this works is because a number of the String.Concat overloads will accept object in the argument list and since an int, float etc. are in essence objects they can be passed to the Concat overload that accepts one or more object parameters. Internally Concat of basically does a .ToString() on the incomming object therefore turning your int into it's string representation.

In your specific example

string st = "" + 12;

The compiler will reconize that the first string is empty and simply call the String.Concat(object) overload. Which will convert the integer 12 to a string and assign it to st.

This overload is called because the integer can be implicitly boxed to fit into the object type and therefore satisfy the method overload selection.

Chris Taylor
  • 52,623
  • 10
  • 78
  • 89
0

Because the compiler will call .ToString() on all objects if one of the parameters is a string.

jgauffin
  • 99,844
  • 45
  • 235
  • 372
0

This is working because of the operator overload of the operator + on the string datatype.

Jan
  • 15,802
  • 5
  • 35
  • 59
0

Because the first is an expression and there C# makes an implicit conversion.

The second is an assignement with a static value, and the static value has no methods to be called.

As long as the value to be asigned is a variable or expression return there will be a toSting method to call.

David Mårtensson
  • 7,550
  • 4
  • 31
  • 47
0

It's because the compiler translates the addition to a call to String.Concat().

Internally, all operands are boxed (if necessary) and passed to the String.Concat method (which of course calls ToString() on all arguments).

Botz3000
  • 39,020
  • 8
  • 103
  • 127