How do I force the Dispose method to be called automatically, as clients can forget to call Dispose method?

Answer

Call the Dispose method in Finalize method and in Dispose method suppress the finalize method
using GC.SuppressFinalize. Below is the sample code of the pattern. This is the best way we do
clean our unallocated resources and yes not to forget we do not get the hit of running the Garbage
collector twice.
public class CleanClass : IDisposable
    {
        public void Dispose()
        {
            GC.SuppressFinalize(this);
        }
        protected override void Finalize()
        {
            Dispose();
        }

    }

All asp Questions

Ask your interview questions on asp

Write Your comment or Questions if you want the answers on asp from asp Experts
Name* :
Email Id* :
Mob no* :
Question
Or
Comment* :
 





Disclimer: PCDS.CO.IN not responsible for any content, information, data or any feature of website. If you are using this website then its your own responsibility to understand the content of the website

--------- Tutorials ---