What is the difference between the dispose and finalize methods in C#? | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 7

What is the difference between the dispose and finalize methods in C#?

What can be the difference between them ?

23rd Aug 2020, 3:24 PM
Miss.Psycho
Miss.Psycho - avatar
7 Réponses
+ 5
files etc…      -  It is automatically raised by garbage collection mechanism whenever the object goes out of scope.      -  This method belongs to object class.      -  We need to implement this method whenever we have unmanaged resources in our code and make sure these resources will be freed when garbage collection process done.      -  It will show effect on performance of website and it will not suitable to free objects immediately.      Example // Implementing Finalize method public class Sample { //At runtime destructor automatically Converted to Finalize method. ~Sample() { // your clean up code } }
24th Aug 2020, 6:56 AM
Sanadu
Sanadu - avatar
+ 4
Dispose() Method      -  This dispose method will be used to free unmanaged resources like files, database connection etc.      -  To clear unmanaged resources we need to write code manually to raise dispose() method.      -  This Dispose() method belongs to IDisposable interface.      -  If we need to implement this method for any custom classes we need to inherit the class from IDisposable interface.      -  It will not show any effect on performance of website and we can use this method whenever we want to free objects immediately. Example //Implement Dispose Method. public class TestDispose : IDisposable { private bool disposed = false; //Implement IDisposable. public void Dispose() { Dispose(true); } protected virtual void Dispose(bool disposing) { if (!disposed) { if (disposing) { // clean unmanged objects } // clean unmanaged objects). disposed = true; } } } Finalize() Method      -  This method also free unmanaged resources like database connections, files e
24th Aug 2020, 6:55 AM
Sanadu
Sanadu - avatar
+ 4
Hope it works ❤️
24th Aug 2020, 6:57 AM
Sanadu
Sanadu - avatar
+ 4
sneeze Implementing Dispose() makes the cleanup of resources deterministic in that the application controls when that cleanup occurs with things like releasing files, network connections, database connection, etc. This is in contrast to waiting for GC to eventually call finalize to do that cleanup resulting in tying resources for milliseconds to seconds. That's expensive if several connections could be handled within milliseconds but they have to wait for resources to be closed and released.
24th Aug 2020, 12:20 PM
David Carroll
David Carroll - avatar
+ 3
They seems to do the same thing. From a historic point if view, I would implement the destructor (~ finalize). Since that is the first way, I learnt to do it. From a convenient point of view I like to implement, dispose. Because than I can use the "using"-statement. When to use dispose above finalize And When to use finalize above dispose
24th Aug 2020, 11:28 AM
sneeze
sneeze - avatar
+ 3
Adu If you copied that from a website, please, just post a link. At the very least, clean up the white space.
24th Aug 2020, 12:04 PM
David Carroll
David Carroll - avatar