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

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.

//Implement Dispose Method.

public class TestDispose : IDisposable


private bool disposed = false;

//Implement IDisposable.

public void Dispose()




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.     


// Implementing Finalize method

public class Sample


//At runtime destructor automatically Converted to Finalize method.



// your clean up code




Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s