String, Why does this work??
-
Hi all,
y = 2;
string yString = y;This of course does not work ,Cannot implicitly convert type 'int' to 'string'. But this is no problem:
y = 2;
string yString = "" + y;Why?? Regards, Grooover
0200 A9 23 0202 8D 01 80 0205 00
-
Hi all,
y = 2;
string yString = y;This of course does not work ,Cannot implicitly convert type 'int' to 'string'. But this is no problem:
y = 2;
string yString = "" + y;Why?? Regards, Grooover
0200 A9 23 0202 8D 01 80 0205 00
Because the designers of .NET C# decided that the "+" operator between strings ... if the item before the plus-sign is a string ... would automatically invoke conversion of all items to the right of the plus-sign to Type String. You can see an even more dramatic example of this if you execute:
int v = 1;
Int32 w = 2;
Int64 x = 3;
double y = 4;
float z1 = 5;
Single z2 = 6;
string yString = "" + v + w + x + y + z1 + z2;Yep, it compiles. So, you can infer that ... given a lack of parentheses) arithmetical operator evaluation for the plus sign is "turned off." If you think this is kind of strange, you may not be alone :)
“I speak in a poem of the ancient food of heroes: humiliation, unhappiness, discord. Those things are given to us to transform, so that we may make from the miserable circumstances of our lives things that are eternal, or aspire to be so.” Jorge Luis Borges
-
Because the designers of .NET C# decided that the "+" operator between strings ... if the item before the plus-sign is a string ... would automatically invoke conversion of all items to the right of the plus-sign to Type String. You can see an even more dramatic example of this if you execute:
int v = 1;
Int32 w = 2;
Int64 x = 3;
double y = 4;
float z1 = 5;
Single z2 = 6;
string yString = "" + v + w + x + y + z1 + z2;Yep, it compiles. So, you can infer that ... given a lack of parentheses) arithmetical operator evaluation for the plus sign is "turned off." If you think this is kind of strange, you may not be alone :)
“I speak in a poem of the ancient food of heroes: humiliation, unhappiness, discord. Those things are given to us to transform, so that we may make from the miserable circumstances of our lives things that are eternal, or aspire to be so.” Jorge Luis Borges
Thank You for your explanation, Learn something every day. thanks
0200 A9 23 0202 8D 01 80 0205 00
-
Hi all,
y = 2;
string yString = y;This of course does not work ,Cannot implicitly convert type 'int' to 'string'. But this is no problem:
y = 2;
string yString = "" + y;Why?? Regards, Grooover
0200 A9 23 0202 8D 01 80 0205 00
The concatenation operators are replaced by calls to one of the overloaded string.Concat methods. There are versions with string or object parameters, e.g Concat(string, string, string), Concat(object, object, object), and if possible the more efficient (string, string, ...) versions will be used. This gives us, well me really, the opportunity to play with ILDASM and have a look at what the C# compiler decides to do.
private static String StringConcat(String a, String b, String c) {
return a + b + c;
}private static String ObjectConcat(String a, int b, DateTime c) {
return a + b + c;
}and the IL shows the use of the different Concat methods.
.method private hidebysig static string StringConcat(string a,
string b,
string c) cil managed
{
// Code size 9 (0x9)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldarg.1
IL_0002: ldarg.2
IL_0003: call string [mscorlib]System.String::Concat(string,
string,
string)
IL_0008: ret
} // end of method ConcatTest::StringConcat.method private hidebysig static string ObjectConcat(string a,
int32 b,
valuetype [mscorlib]System.DateTime c) cil managed
{
// Code size 19 (0x13)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldarg.1
IL_0002: box [mscorlib]System.Int32
IL_0007: ldarg.2
IL_0008: box [mscorlib]System.DateTime
IL_000d: call string [mscorlib]System.String::Concat(object,
object,
object)
IL_0012: ret
} // end of method ConcatTest::ObjectConcatAlan.
-
Because the designers of .NET C# decided that the "+" operator between strings ... if the item before the plus-sign is a string ... would automatically invoke conversion of all items to the right of the plus-sign to Type String. You can see an even more dramatic example of this if you execute:
int v = 1;
Int32 w = 2;
Int64 x = 3;
double y = 4;
float z1 = 5;
Single z2 = 6;
string yString = "" + v + w + x + y + z1 + z2;Yep, it compiles. So, you can infer that ... given a lack of parentheses) arithmetical operator evaluation for the plus sign is "turned off." If you think this is kind of strange, you may not be alone :)
“I speak in a poem of the ancient food of heroes: humiliation, unhappiness, discord. Those things are given to us to transform, so that we may make from the miserable circumstances of our lives things that are eternal, or aspire to be so.” Jorge Luis Borges
Thing of beauty, .NET.
Regards, Rob Philpott.