Good samples of using Finalizers in C#

后端 未结 2 1825
深忆病人
深忆病人 2020-12-31 10:13

When I read a few articles about memory management in C#, I was confused by Finalizer methods.

There are so many complicated rules which related with them. For inst

2条回答
  •  渐次进展
    2020-12-31 10:30

    The best practice known to me is plain simple don't use them. There might however be some corner cases when you want to use a finalizer, particularly when dealing with unmanaged objects and you can't implement Dispose pattern (I do not know legacy issues) then you can implement Finalize method with caution (and it could reduce the performance of your system, make your objects undead and other possibly weird scenarios, minding the exceptions as they are uncatchable:)).

    In 99% of cases just write the use Dispose pattern and use this method to clean after yourself and everything will be fine.

提交回复
热议问题