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 do u deploy ur project?

2213


if i want to give an alert message like "try after sometime" to a web page which is being seen by other person.if a web page is not seen by anyone then it should display otherwise it show a display a message stating that other person is viewing so try after some time........how can i implement this.

1830


They mostly asked difference between versions of technologies

934


when a request is made in Life cycle of ASP.NET page .

525


What is the benefit of WebAPI over WCF?

559






How does asp.net page work?

520


Explain model, view and controller represent in an mvc application? : asp.net mvc

508


Can we make activex dll also ti execute in some process as that of client ? How can we do?

4242


What is Razor View Engine

601


How u refer webservices?

1483


What is a master page and what does it do?

529


How may clustered index we can create in table?

557


What is the use of session state and application state and difference between them?

473


Out of ASP or ASP.NET which one is stateless?

674


What is data binding in asp net?

581