从IL看C#

 1 using System;
 2 
 3 namespace ConsoleApplication1 {
 4     class TestClass {
 5         static void Main()
 6         {
 7             string a = "hello";
 8             a += "world";
 9             Console.WriteLine(a);
10         }
11     }
12 
13 }
//.method 表示这是一个方法
//hidebysig=hide by signature 隐藏掉基类中相同签名的方法
//cil managed=common intermediate language managed code 也就是说该方法的代码是通用中间语言托管代码
.method private hidebysig static void  Main() cil managed
{
  .entrypoint //这是一个入口方法
  // 代码大小(该方法的代码所占的存储空间,单位:byte)       27 (0x1b)
  .maxstack  2 //栈高为2
  .locals init ([0] string a) //初始化局部变量(编号 类型 变量名)
  IL_0000:  nop    //占位符(no operation)
  IL_0001:  ldstr      "hello"    //load string 把‘hello’载入内存中(栈中)
  IL_0006:  stloc.0    //stack(to)local 从栈中 弹出 数据 放到 局部变量[0]=string a
  IL_0007:  ldloc.0    //load local(to stack)
  IL_0008:  ldstr      "world"    //load string 把‘world’载入内存中(栈中)
  IL_000d:  call       string [mscorlib]System.String::Concat(string,
                                                              string)    //调用string.concat(string,string)
  IL_0012:  stloc.0
  IL_0013:  ldloc.0
  IL_0014:  call       void [mscorlib]System.Console::WriteLine(string)
  IL_0019:  nop
  IL_001a:  ret
} // end of method TestClass::Main

我认为这其中有个秘密,但不知是我误认为,所以不敢冒然说出,如果顶帖的人达到五十,我会豁出去讲的。看大家的意思咯。

原文地址:https://www.cnblogs.com/onlyme/p/2514316.html