top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration

Facebook Login
Site Registration

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

0 votes
132 views
How do I force the Dispose method to be called automatically, as clients can forget to call the Dispose method?
posted Sep 14, 2015 by Sathaybama

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Call the Dispose method in the Finalize method and in the Dispose method to suppress the Finalize method from using "GC.SuppressFinalize". The following is the sample code of the pattern. This is the best way to clean our unallocated resources, and yes do not 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();
        }
    }
answer Sep 15, 2015 by Shivaranjini
Similar Questions
+3 votes

How we can force a function to be called only once. I can do this using some static variable or using boost library, but is there some other way to do this as I don't want to use any of the earlier way. Function is a non member function of any class.

Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...