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 are the types of ASP objects ?

7 Answers   Satyam,


How does a web application session work?

0 Answers  


Does C# support static class?Is Static class be inherited by any class?

2 Answers  


What is a 1x1 pixel?

0 Answers  


Explain < @OutputCache% > and the usage of VaryByParam, VaryByHeader ?

1 Answers  






Differentiate an ADO.NET Dataset and an ADO Recordset with its functionality?

3 Answers   Siebel,


What is the question mark in a url?

0 Answers  


What are the various ways of securing a web site that could prevent from hacking etc ?

1 Answers  


Explain what is postback in asp. Net?

0 Answers  


how to increase performance of web site? if there is a page with high load. the content is high then what should we do to increase performance?

3 Answers   Emphasis, Mphasis, TCS,


Explain what are webservices?

0 Answers  


how to elimainte the similar data from the different tables

0 Answers  


Categories