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

Answer Posted / 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       View All Answers


Please Help Members By Posting Answers For Below Questions

What is filters in web api?

504


Is razor a server side?

523


What is orm entity framework?

515


How we can call a JavaScript function on the change of a Dropdown List in ASP.Net MVC?

554


Explain dependency resolution?

612






What is the .net framework 3.0 (formerly winfx)?

545


What is basic authentication in web api?

523


What are Code Blocks in Views?

656


Describe the .net framework architecture.

568


Explain test driven development (tdd) ?

557


Can I uninstall microsoft .net framework?

543


My organization went through the approval process of supporting the .net framework 2.0 in production. Do we need to go through the same process all over again for the .net framework 3.0? Do I need to do any application compatibility testing for my .net framework 2.0 applications?

544


why DotNetFramework is included in building a software

2202


What are the options can be configured in AJAX helpers?

580


What is edm (entity data model)? : Entity framework

539