static void Main(string[] args)
{
int i;
string text = "";
for (i = 0; i < 10; i++)
{
text = "Line " + i;
Console.WriteLine("{0}", text);
}
Console.WriteLine("last text output in loop:{0}", text);
Console.ReadKey(); }
为什么这段代码也能编译,text是string型,而i是int型,text = "Line " + i;却没有错误
{
int i;
string text = "";
for (i = 0; i < 10; i++)
{
text = "Line " + i;
Console.WriteLine("{0}", text);
}
Console.WriteLine("last text output in loop:{0}", text);
Console.ReadKey(); }
为什么这段代码也能编译,text是string型,而i是int型,text = "Line " + i;却没有错误
-------------------------------------------------------------------------
xule即时通讯软件,可定制的P2P即时通信系统为各行业门户网站和企事业单位打造一个稳定,安全,高效,可扩展的即时通信系统,类似QQ、贸易通和淘宝旺旺(可提供技术资料和源码)
介绍:http://www.xulesoft.cn
QQ:1196435261
string text = "Line " + i;但是解释不了:
int text = "123" + i;应该从表达式树开始。
<code>
//[1]text为string类型
//[2]string类型被 using string = System.String
//[3]左表达式希望String
//[4]+是表达式方法:public static string Concat(object arg0);
//[5]i-->arg0引发了隐式的转换(implicit)
//[6]装箱obj o = i;
//[7]Concat函数调用o.ToString
//[8]实际执行obj.ToString()
//[9]obj.ToString()被override重写为System.Int32.ToString();
//[10]...后面的就不用解释地吧~~
</code>