### 結構二三事 (1)

• 640
• 0
• 2020-04-10

  static void Main(string[] args)
{
var s1 = 1.ToString();
Console.WriteLine(s1);
var s2 = 1.ToString();
Console.WriteLine(s2);
}


.method private hidebysig static void  Main(string[] args) cil managed
{
.entrypoint
// 程式碼大小       36 (0x24)
.maxstack  1
.locals init ([0] string s1,
[1] string s2,
[2] int32 V_2)
IL_0000:  nop
IL_0001:  ldc.i4.1
IL_0002:  stloc.2
IL_0003:  ldloca.s   V_2
IL_0005:  call       instance string [mscorlib]System.Int32::ToString()
IL_000a:  stloc.0
IL_000b:  ldloc.0
IL_000c:  call       void [mscorlib]System.Console::WriteLine(string)
IL_0011:  nop
IL_0012:  ldc.i4.1
IL_0013:  stloc.2
IL_0014:  ldloca.s   V_2
IL_0016:  call       instance string [mscorlib]System.Int32::ToString()
IL_001b:  stloc.1
IL_001c:  ldloc.1
IL_001d:  call       void [mscorlib]System.Console::WriteLine(string)
IL_0022:  nop
IL_0023:  ret
} // end of method Program::Main


 static void Main(string[] args)
{
int V_2;
V_2 = 1;
var s1 =V_2.ToString();
Console.WriteLine(s1);
V_2 = 1;
var s2 = V_2.ToString();
Console.WriteLine(s2);
}


 static void Main(string[] args)
{
int V_2;
V_2 = 1;
var s1 = V_2.ToString();
Console.WriteLine(s1);
var s2 = V_2.ToString();
Console.WriteLine(s2);
}

If M is an instance function member declared in a value-type:

If E is not classified as a variable, then a temporary local variable of E’s type is created and the value of E is assigned to that variable. E is then reclassified as a reference to that temporary local variable. The temporary variable is accessible as this within M, but not in any other way. Thus, only when E is a true variable is it possible for the caller to observe the changes that M makes to this.

  class MyClass
{
public MyClass(int a)
{
x = a;
}

public void Request()
{
Console.WriteLine(x.ToString());
}
}

struct MyStruct
{
public MyStruct(int a)
{
x = a;
}

public void Request()
{
Console.WriteLine(x.ToString());
}
}
MyClass 建構式的 IL Code：
.method public hidebysig specialname rtspecialname
instance void  .ctor(int32 a) cil managed
{
// 程式碼大小       16 (0x10)
.maxstack  8
IL_0000:  ldarg.0
IL_0001:  call       instance void [mscorlib]System.Object::.ctor()
IL_0006:  nop
IL_0007:  nop
IL_0008:  ldarg.0
IL_0009:  ldarg.1
IL_000a:  stfld      int32 ConsoleApp2.Program/MyClass::x
IL_000f:  ret
} // end of method MyClass::.ctor
MyClass.Request 方法的 IL Code：
.method public hidebysig instance void  Request() cil managed
{
// 程式碼大小       22 (0x16)
.maxstack  1
.locals init ([0] int32 V_0)
IL_0000:  nop
IL_0001:  ldarg.0
IL_0002:  ldfld      int32 ConsoleApp2.Program/MyClass::x
IL_0007:  stloc.0
IL_0008:  ldloca.s   V_0
IL_000a:  call       instance string [mscorlib]System.Int32::ToString()
IL_000f:  call       void [mscorlib]System.Console::WriteLine(string)
IL_0014:  nop
IL_0015:  ret
} // end of method MyClass::Request

o    If T is a class-type and I identifies an instance field of that class-type:
•    If the value of E is null, then a System.NullReferenceException is thrown.
•    Otherwise, if the field is readonly and the reference occurs outside an instance constructor of the class in which the field is declared, then the result is a value, namely the value of the field I in the object referenced by E.
•    Otherwise, the result is a variable, namely the field I in the object referenced by E

o    If T is a struct-type and I identifies an instance field of that struct-type:
•    If E is a value, or if the field is readonly and the reference occurs outside an instance constructor of the struct in which the field is declared, then the result is a value, namely the value of the field I in the struct instance given by E.
•    Otherwise, the result is a variable, namely the field I in the struct instance given by E.

.method public hidebysig instance void  Request() cil managed
{
// 程式碼大小       19 (0x13)
.maxstack  8
IL_0000:  nop
IL_0001:  ldarg.0
IL_0002:  ldfld      object ConsoleApp2.Program/MyClass2::x
IL_0007:  callvirt   instance string [mscorlib]System.Object::ToString()
IL_000c:  call       void [mscorlib]System.Console::WriteLine(string)
IL_0011:  nop
IL_0012:  ret
} // end of method MyClass2::Request