//测试代码 internal interface ITest1 { void Test1(); } class A:ITest1 { public void Test1(){} } struct B:ITest1 { public void Test1(){} } static class App { public static int Main() { ITest1 a, b; A class_a = new A(); B struct_b = new B(); Console.WriteLine(struct_b); Console.WriteLine(class_a); a = class_a; b = struct_b; Console.WriteLine(b); Console.WriteLine(a); return 0; } }
IL代码,可以很明确的看出接口是引用类型
.method public hidebysig static int32 Main () cil managed { // 方法起始 RVA 地址 0x2060 // 方法起始地址(相对于文件绝对值:0x0260) // 代码长度 65 (0x41) .maxstack 1 .entrypoint .locals init ( [0] class CSharp1.ITest1,、、、、 [1] class CSharp1.ITest1,、 [2] class CSharp1.A, [3] valuetype CSharp1.B,K [4] int32 ) // 0x026C: 00 IL_0000: nop // 0x026D: 73 03 00 00 06 IL_0001: newobj instance void CSharp1.A::.ctor() // 0x0272: 0C IL_0006: stloc.2 // 0x0273: 12 03 IL_0007: ldloca.s 3 // 0x0275: FE 15 04 00 00 02 IL_0009: initobj CSharp1.B // 0x027B: 09 IL_000f: ldloc.3 // 0x027C: 8C 04 00 00 02 IL_0010: box CSharp1.B//装箱操作 // 0x0281: 28 06 00 00 0A IL_0015: call void [mscorlib]System.Console::WriteLine(object) // 0x0286: 00 IL_001a: nop // 0x0287: 08 IL_001b: ldloc.2 // 0x0288: 28 06 00 00 0A IL_001c: call void [mscorlib]System.Console::WriteLine(object) // 0x028D: 00 IL_0021: nop // 0x028E: 08 IL_0022: ldloc.2 // 0x028F: 0A IL_0023: stloc.0 // 0x0290: 09 IL_0024: ldloc.3 // 0x0291: 8C 04 00 00 02 IL_0025: box CSharp1.B // 0x0296: 0B IL_002a: stloc.1 // 0x0297: 07 IL_002b: ldloc.1 // 0x0298: 28 06 00 00 0A IL_002c: call void [mscorlib]System.Console::WriteLine(object) // 0x029D: 00 IL_0031: nop // 0x029E: 06 IL_0032: ldloc.0 // 0x029F: 28 06 00 00 0A IL_0033: call void [mscorlib]System.Console::WriteLine(object) // 0x02A4: 00 IL_0038: nop // 0x02A5: 16 IL_0039: ldc.i4.0 // 0x02A6: 13 04 IL_003a: stloc.s 4 // 0x02A8: 2B 00 IL_003c: br.s IL_003e // 0x02AA: 11 04 IL_003e: ldloc.s 4 // 0x02AC: 2A IL_0040: ret } // 方法 App::Main 结束DS