Since .NET has a garbage collector why do we need finalizers/destructors/dispose-pattern?

前端 未结 12 1561
粉色の甜心
粉色の甜心 2020-12-07 09:09

If I understand correctly the .net runtime will always clean up after me. So if I create new objects and I stop referencing them in my code, the runtime will clean up those

12条回答
  •  有刺的猬
    2020-12-07 09:53

    There are a few (very few) cases where it may be necessary to perform a specific action when a pure managed object is no longer used, I can't come up with an example off the top of my head but I have seen a couple of legitimate uses over the years. But the main reason is to clean up any unmanaged resources that the object might be using.

    So, in general, you won't need to use the Dispose/Finalize pattern unless you are using unmanaged resources.

提交回复
热议问题