What is the Difference B/W Finalize() and Dispose() in .Net?

Answer Posted / karna

when we write the destructor in c#,the runtime replaces the
destructor with the finalize method in IL code,so we dont
know at what time the destructor is called by the garbage
collector.so the finalizer is non deterministic.if we want
to do the garbage collection for unmanages resources,we
have to implement our own destructor or finlizer
method.but,the runtime will take two round trips to remove
those objects from the heap.if runtime calls the own
destructor,it will take onely one round trip.

to remove the unmanaged resources without the performance
issues,we have to inherit the System.Idisposable interface
and implement the Dispose() method.here one problem is
there,we have to write a code in a way that,the dsipose
method must and should execute.
for that reason we have to keep the dispose methos for the
objcets in the finally block.

problem with the dispose is that,some times because of
some problmes dispose() methos may not execute.
so the good practice is to use both to achieve the desied
performance.

Is This Answer Correct ?    20 Yes 3 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How can you register a custom server control to a web page?

549


What are sessions used for?

561


What is the function used for removing an event listener?

551


What is HTTPModule and HTTPcontext? What is the use of each?

1996


What is the difference between user control and custom control?

486






What is the purpose of url encoding?

498


What is a form tag?

511


What is postback and autopostback in asp.net?

532


Why select Web API?

568


How does output caching work in ASP.NET?

504


Explain Areas in MVC?

656


Difference between application events and session events

596


How to do parallel database export in remote SQL Server in ESSL time track. It is working for local server but not working for remote SQL Server

5693


Which two new properties are added in asp.net 4.0 page class?

529


Can I recieve both html markup for page and code in the asp.net web page's source code portion in the web browser?

538