Thursday, May 28, 2020

Difference Between dispose() and finalize() in C#

Definition of dispose()

The dispose() method releases the unmanaged resources that are held by an object of the class. The unmanaged resources are files, data connections, etc. The method dispose() is declared in the interface IDisposeable and it is implemented by the class by implementing the interface IDisposable. This method is not called automatically. The programmer has to implement it manually when you are creating a custom class that will be used by others.

The method has the following syntax:

  1. public void dispose( ){
  2. // Dispose code here
  3. }

In the above syntax, you can observe that the method is declared as public. It is because this method is defined in the interface IDisposable and it has to be implemented by the class that implements this interface. So, to provide accessibility to the implementing class, the method is declared as public.

This method is invoked manually by the code of a program as it is implemented to invoke. The methods performance is fast, and it instantly frees the resources held by the object of a class.

Definition of finalize()

The finalize() method is defined in the object class. It is used for cleanup activities. This method is called by the garbage collector when the reference of an object is not used for a long time. Garbage collector frees the managed resources automatically but if you want to free the unmanaged resources like file handle, data connection, etc., the finalize method has to be implemented manually. The garbage collector invokes the method finalize() just before it destroys the object completely.

The syntax of the method finalize():

  1. protected void finalize( ){
  2. // finalization code here
  3. }

In the syntax above, the method finalize() is declared as protected. The reason behind this is, the method finalize() must not be accessible from outside the class, and it must only be accessible to the garbage collector.

The finalize() method affects the cost of the performance as it does not free the memory instantly. In C# the finalize method is called automatically with destructors.

Key Differences Between dispose() and finalize()

  1. The method dispose() is defined in an interface IDisposable. On the other hand, the method finalize() is defined in the class object.
  2. The method dispose() has to be manually invoked inside the code by a programmer, while the method finalize is automatically invoked by the garbage collector before it destroys the object.
  3. The method dispose could be invoked anytime, whereas the method finalize is invoked by the garbage collector when it finds that that object has not been referenced for a long time.
  4. The method dispose() is implemented in a class after implementing the interface IDisposable. The method finalize() has to be implemented only for unmanaged resources because the managed resources are automatically freed by the garbage collector.
  5. The access specifier of the method dispose() is public as it is defined in the interface IDisposable and it would be implemented by the class that implements this interface hence, it should be public. On the other hand, the method finalize() has protected access specifier so that it should not be accessible to any member outside the class.
  6. The method dispose() is fast and frees the object instantly hence, it does not affects the performance cost. The method finalize() is slower and does not free the resources held by the object instantly.

No comments:

Post a Comment