I have been programming in .NET for four years (mostly C#) and I use IDiposable extensively, but I am yet to find a need for a finaliser. What are finalisers for?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
A finalizer is a last ditch attempt to ensure that something is cleaned up correctly, and is usually reserved for objects that wrap unmanaged resources, such as unmanaged handles etc that won’t get garbage collected.
It is rare indeed to write a finalizer. Fortunately (and unlike
IDisposable), finalizers don’t need to be propagated; so if you have aClassAwith a finalizer, and aClassBwhich wrapsClassA, thenClassBdoes not need a finalizer – but quite likely bothClassAandClassBwould implementIDisposable.For managed code,
IDisposableis usually sufficient. Even if you don’t clean up correctly, eventually the managed objects will get collected (assuming they are released).