难得还有人关注,帮我顶,现在我自己分析了一下,请大家指点一下对不对。看一个例子: Console.WriteLine("my" + 4); 输出: my4 因为WriteLine()需要接受一个string对象,可是现在只有一个string和一个int型的。于是C#编译器自动调用String对象的静态Concat方法的一个版本。代码如下public static String Concat(Object arg0, Object arg1)来连接成一个string。其中的 arg1位置为一个int 型的4,所以4被“装箱”操作。开头我问的问题 childForm.Text=childForm.Text + childCount;解释为childForm.Text="HELLO"+ 4 ; 这里是不是也像WriteLine一样编译器调用了Concat("HELLO",4),变成了childForm.Text=Concat("HELLO",4)呢?个人意见,请大家指正。
source code:
=================================
string s = 4 + "aaa";
Console.WriteLine(s);IL code:
=================================
IL_0000: ldc.i4.4
IL_0001: box [mscorlib]System.Int32
IL_0006: ldstr "aaa"
IL_000b: call string [mscorlib]System.String::Concat(object,
object)
IL_0010: stloc.0
IL_0011: ldloc.0
IL_0012: call void [mscorlib]System.Console::WriteLine(string)