Reputation:
I was learning Destructors, when I got a question -> When do we need to use destructors? and also at the same time, I was like Thinking on How these can be used in real life.
So Can anyone give a real time example on how is Destructors used?
Upvotes: 0
Views: 56
Reputation: 38094
There is a very good article by Eric Lippert. This is about finalizer/destructor and it has some tests. One of the test says:
When finalizers are being run because a process is being shut down, the runtime sets a limit on how much time the finalizer thread gets to spend making a good-faith effort to run all the finalizers. If that limit is exceeded then the runtime simply stops running more finalizers and shuts down the program
So it can be concluded that finalizers can be avoided to be written almost on all cases.
Read more about finalizer by Jon Skeet:
You should almost never use them. Basically you should only need them if you have a direct handle on an unmanaged resource, and not only is that incredibly rare, but using SafeHandle as a tiny level of indirection is a better idea anyway (which handles clean-up for you)
Upvotes: 1