Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

C# - Difference between Dispose and Finalize Method in C# with Example

Feb 21, 2015
Introduction:

Here I will explain difference between dispose and finalize methods in c#, vb.net with example or dispose() vs finalize() methods in asp.net or use of dispose and finalize methods in asp.net using c#, vb.net with example. Generally we will use dispose and finalize methods to destroy unmanaged objects.

Description:


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 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
}
}


If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

3 comments :

Mr. verma said...

very useful info

Anonymous said...

Nice one

Barath.D said...

Good article to understand the clear difference between dispose and finalize. Thanks for posting the blog.

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.