Posts

Showing posts from October, 2014

Difference between Destructor, Dispose and Finalize

Destructor They are special method that contains clean up code for the object.We can't call them explicitly in our code as they are implicitly called by GC(Garbage Collector).In c# they have same name as the class name preceded by the "~" sign. class MyClass     {         public MyClass()         { }         ~MyClass()         { }     } Dispose These are like any other methods in the class and can be called explicitly but they have a special purpose of cleaning up the object.In the dispose method we write clean up code for the object.It is important that we freed up all the unmanaged resources in the dispose method like database connections,files, etc.. The class implementing dispose method should implement IDisposable which is inherited by interface and it contains GC.SupressFinalize() method for the object it is disposing if the class has destructor because it has already done the work to clean up the object ,then it is not necessary for the garbage colle