Difference between Dispose and Finalize Method in C# with Example
Difference between Dispose and Finalize Method in C# with Example
Other
Asp.net related Examples Post which are consider in this asp.net blog:
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 of Dispose:
//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 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 of Finalize:
// implementing Finalize method
public class Sample
{
//At runtime destructor automatically converted to finalize
method.
~Sample ()
{
// your clean up code
}
}
Asp.net Related Other Post:
- What is MVC model
- Introduction of SQL
- Textbox Asp.net control
- ImageButton control
- Asp.net Button control.
- Literal control in Asp.net
- Mvc version, and introduction
- Asp.net Ajax Json Introduction
- Image size before Uploading.
- Asp.net CheckBoxList using jQuery.
- Cropping image using jQuery in asp.net
- Asp.net Imagebutton control example
- C# Variables,How to declare and use in C#:
- JQuery Change Div Background Color Randomly
- Displaying the textbox value in javascript Messagebox
- Get selected radio button values using JQuery.
- How do you do html text encodes using JavaScript
- Limit Number of Characters in a TextArea using jQuery
- jquery disable or Enable submit button after validation
- Enable Disable Submit Button using jQuery
- JQuery UI Datepicker (Calendar) with asp.net textbox
- Get current datetime in jquery and javaScript.
- jQuery modal dialog with postback in asp.net
- Example of C# for Bind Data to asp.net Textbox inside gridview control
- Bind Data to asp.net textbox control in inside of gridview Using C# Example
Comments
Post a Comment