What is the difference between Finalize and Dispose (Garbage collection)



What is the difference between Finalize and Dispose (Garbage collection)..

Answer / kirti

Class instances often encapsulate control over resources that are not managed by the runtime, such as window handles (HWND), database connections, and so on. Therefore, you should provide both an explicit and an implicit way to free those resources. Provide implicit control by implementing the protected Finalize Method on an object (destructor syntax in C# and the Managed Extensions for C++). The garbage collector calls this method at some point after there are no longer any valid references to the object. In some cases, you might want to provide programmers using an object with the ability to explicitly release these external resources before the garbage collector frees the object. If an external resource is scarce or expensive, better performance can be achieved if the programmer explicitly releases resources when they are no longer being used. To provide explicit control, implement the Dispose method provided by the IDisposable Interface. The consumer of the object should call this method when it is done using the object.

Dispose can be called even if other references to the object are alive. Note that even when you provide explicit control by way of Dispose, you should provide implicit cleanup using the Finalize method. Finalize provides a backup to prevent resources from

permanently leaking if the programmer fails to call Dispose.

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Dot Net Framework Interview Questions

What is work of clr?

0 Answers  


What are HTML Helpers, AJAX Helpers in ASP.Net MVC?

0 Answers  


I want ask from plz smaple example code for Biztalkk server

0 Answers  


Can you set the unlimited length for "maxjsonlength" property in config?

0 Answers  


What is the use of action filters in an mvc application?

0 Answers  






Is dapper faster than entity framework?

0 Answers  


What is strong name and which tool is used for this ?

4 Answers   Accenture, BirlaSoft,


What are the versions of .net framework?

0 Answers  


What are the ways to show data grid inside a data grid for a master details type of tables?

1 Answers  


When will the .net framework 3.0 be released?

0 Answers  


What are the types of authentication in .net

1 Answers  


What is the difference between using system.net and system.net.sockets?

1 Answers  


Categories