Finalize在c#编程语言中需要特殊语法,因此,c#要求在类名前加~符号来定义Finalize方法:例如
internal class FinalizeDemo { ~FinalizeDemo() { int num = GC.MaxGeneration; } }
ILDasm.exe所在目录
C:Program Files (x86)Microsoft SDKsWindowsv10.0AinNETFX 4.6 Tools
使用ILDasm.exe查看Finalize生成的中间语言
.method family hidebysig virtual instance void Finalize() cil managed { .override [mscorlib]System.Object::Finalize // 代码大小 19 (0x13) .maxstack 1 .locals init ([0] int32 num) IL_0000: nop .try { IL_0001: nop IL_0002: call int32 [mscorlib]System.GC::get_MaxGeneration() IL_0007: stloc.0 IL_0008: leave.s IL_0012 } // end .try finally { IL_000a: ldarg.0 IL_000b: call instance void [mscorlib]System.Object::Finalize() IL_0010: nop IL_0011: endfinally } // end handler IL_0012: ret } // end of method FinalizeDemo::Finalize