Q:

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

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

The finalize and dispose methods are used to free unmanaged resources. There are some differences between a finalize and dispose that I have mentioned below.

Finalize:

  • Finalize is used to free unmanaged resources that are not in use, like files, database connections in the application domain and more. These are resources held by an object before that object is destroyed.
  • In the Internal process, it is called by Garbage Collector and can’t be 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 that are not in use like files, database connections in the Application domain at any time.
  • Dispose is explicitly called by manual user code.
  • If we need to use the dispose method, we must implement that class via IDisposable interface.
  • It belongs to IDisposable interface.
  • Implement this when you are writing a custom class that will be used by other users.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# Interview Questions and Answers,You Need To Know

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
What are partial classes?... >>
<< Why Properties are introduced in C#?...