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

Answers were Sorted based on User's Feedback



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

Answer / sreeram pavan

Both are ways to destroy the object. By object i mean when
you have some unmanaged resources used in your class, you
have to make sure that you write the logic to destroy them
in the finalize() method. But this is the implicit way of
destroying the unmanaged resources, as finalize is called by
garbage collector when it find thats there is no reference
to this object from the stack.
There is an explicit way to destroy the unmanaged resources.
That is by implementing IDisposable interface. By
implementing this interface, ie you have to write the code
to destroy the resource in Dispose() method, you can call
the object to destroy itself when ever required in your code.

Is This Answer Correct ?    51 Yes 5 No

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

Answer / 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

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

Answer / vivek jagga

The GC call the Finalize() function automatically to destroy
the object called implicit destroy. when you want to destroy
a objects that you think no longer need and free it from
memory, then we will use the dispose function. For better
performance we will use the dispose function explicitly.

Is This Answer Correct ?    21 Yes 5 No

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

Answer / prasanna

.NET provides "finalize" method in which clean up our
resources.but this is always not good so the best is to
implement interface and implement the "Dispose" method
where you can put your clean up routines

Is This Answer Correct ?    23 Yes 8 No

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

Answer / rahupathi

Finalize and dispose are used to clean the objects from
memory. But Finalize will be called based on the GC. If you
finalize any object that will be moved to Finalize queaue.
When the time of the round trip of the GC. It will remove
the objects from Finlize queaue.So,it will not destroy the
object immediately. Unless dispose method will remove the
object immediatley without considering the GC.

Is This Answer Correct ?    16 Yes 3 No

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

Answer / purushottam kumar, mcad, mcsd

Finalize :
1.Finalize() is called by the runtime
2.Is a destructor, called by Garbage Collector when the
object goes out of scope.
3.Implement it when you have unmanaged resources in your
code, and want to make sure that these resources are freed
when the Garbage collection happens.

Dispose :
1.Dispose() is called by the user
2.Same purpose as finalize, to free unmanaged resources.
However, implement this when you are writing a custom class,
that will be used by other users.
3.Overriding Dispose() provides a way for the user code to
free the unmanaged objects in your custom class.

Is This Answer Correct ?    9 Yes 0 No

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

Answer / nitin kumar

Dispose() Method is used to release Unmanage Resources.
When Unmanage Resources are no longer in use and Referenced
and is called Manually.


and

Finalize() Method is similar to Dispose Method But it is
called by the Garbage Collector.

Is This Answer Correct ?    9 Yes 6 No

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

Answer / rajkumar

If we want to delete resources(objects) those are not using,
u should not worry about that GC implicit y call Finalize()
method and remove all such object but if u want to delete
object forcefully(The large obj u want to delete after
completely task) then u can explicity call Dipose() method.

Is This Answer Correct ?    1 Yes 0 No

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

Answer / planet of coders

To getting a great answer please visit this URL : http://planetofcoders.com/difference-finalize-dispose/

Is This Answer Correct ?    1 Yes 0 No

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

Answer / m.sivakumar

Dispose() is called by as an indication for an object to release any unmanaged resources it has held.

Finalize() is used for the same purpose as dispose however finalize doesn’t assure the garbage collection of an object.

Dispose() operates determinalistically due to which it is generally preferred.

Is This Answer Correct ?    2 Yes 2 No

Post New Answer

More ASP.NET Interview Questions

What is the difference between page-level caching and fragment caching?

0 Answers  


if u r projet consist of 4 people means how can u manage the file

10 Answers   Mind Tree,


can any one find and tell the difference between dot net and php which one is best ? which one we get more salary? which one is stable and which one is best for freshers and also better in future and carrer ? which one we wil get more salary sir ? please send ur valuable suggestions to kiranpulsar2007@gmail.com

0 Answers  


What is inproc mode in session?

0 Answers  


What does asp in asp.net stand for?

0 Answers  






i cant get Primary output selection from the Dialog box While Creating Setup and Deployment the web application how to solve it..

1 Answers  


What is difference between singleton and single call?

0 Answers  


Is it necessary to create a Windows application or Web application to test a Web service ? Is it must to consume this service?

2 Answers   Siebel,


what is the uses Of Global.asax

3 Answers  


Is data edited in the Repeater control?

0 Answers   Siebel,


What is custom attribute?

0 Answers  


Which validator control you use if you need to make sure the values in two different controls matched?

0 Answers  


Categories