Why does the Finalize/Destructor example not work in .NET Core?

后端 未结 2 2013
南笙
南笙 2020-12-30 02:43

I\'m trying to learn how finalization and destructor works in C#, I tried to run the code in the System.Object.Finalize example(code copy-pasted, no changes made), but the o

2条回答
  •  隐瞒了意图╮
    2020-12-30 03:09

    Finalization won't occur until the garbage collector runs. Garbage collection doesn't run unless it needs to (e.g. you're low on memory), or if you force it to run.

    Try adding

    System.GC.Collect();
    

    to your code and see if the finalizer runs in that situation.

提交回复
热议问题