+ 7
What is the difference between the dispose and finalize methods in C#?
What can be the difference between them ?
7 ответов
+ 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
}
}
+ 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
+ 4
Hope it works ❤️
+ 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.
+ 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
+ 3
Adu If you copied that from a website, please, just post a link. At the very least, clean up the white space.
+ 1
Dispose and finalize are rarely used in c#, since the garbage collector does the memory management.
However if you work with unmanaged memory it can be usefull.
https://www.oreilly.com/library/view/programming-c/0596001177/ch04s04.html
https://www.c-sharpcorner.com/UploadFile/nityaprakash/back-to-basics-dispose-vs-finalize/
https://dzone.com/articles/when-and-how-to-use-dispose-and-finalize-in-c
https://techdifferences.com/difference-between-dispose-and-finalize-in-c-sharp.html
https://www.dotnettricks.com/learn/netframework/difference-between-finalize-and-dispose-method