Saturday, May 5, 2018

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

finalizer and dispose both are used for same task like to free unmanaged resources but have some differences see. 

Finalize:
  • Finalize used to free unmanaged resources those are not in use like files, database connections in application domain and more, held by an object before that object is destroyed.
  • In the Internal process it is called by Garbage Collector and can’t called manual by user code or any service.
  • Finalize belongs to System.Object class.
  • Implement it when you have unmanaged resources in your code, and make sure that these resources are freed when the Garbage collection happens.
Dispose:
  • Dispose is also used to free unmanaged resources those are not in use like files, database connections in Application domain at any time.
  • Dispose explicitly it is called by manual user code.
  • If we need to dispose method so must implement that class by IDisposable interface.
  • It belongs to IDisposable interface.
  • Implement this when you are writing a custom class that will be used by other users.

No comments:

Post a Comment