In C# you can implicitly concatenate a string and let’s say, an integer:
string sth = "something" + 0;
My questions are:
-
Why, by assuming the fact that you can implicitly concatenate a string and an int, C# disallows initializing strings like this:
string sth = 0; // Error: Cannot convert source type 'int' to target type 'string' -
How C# casts 0 as string. Is it
0.ToString()or(string)0or something else? - How to find an answer of the previous question?
It compiles to a call to
String.Concat(object, object), like this:(Note that this particular line will actually be optimized away by the compiler)
This method is defined as follows: (Taken from the .Net Reference Source)
(This calls
String.Concat(string, string))To discover this, you can use
ildasm, or Reflector (in IL or in C# with no optimizations) to see what the+line compiles to.