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

What is difference cookie and session?

525


How can you handle errors in Web API?

589


Which is better viewstate or session?

513


How to find out what version of asp.net I am using on my machine?

579


How may clustered index we can create in table?

562






What is a web based system?

462


Which method is used to enforce garbage collection in .net?

423


Is asp.net a language?

522


List the advantages and disadvantages of user control an custom control?

534


Is asp.net web forms dead?

511


Give 2 examples for scenarios when routing is not applied?

596


What is scope of an application variable in asp.net?

490


What are the different types of validation controls provided in ASP.NET?

663


Is there any alternative to avoid name collisions other then Namespaces?

570


What are the merits and demerits of viewstate?

579