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 method used to load generated dataset with data on the DataAdapter control ?

3 Answers   Siebel,


How ASP and ASP.NET page works? Explain about asp.net page life cycle?

0 Answers  


Difference b/w casting and boxing?

5 Answers   IBS,


what are debugging types in .net?

2 Answers   ABC, NIIT,


I M working in 1.1 .net framework. In a page I have 5 text box & 2 data grid. In PageLoad() All text box will populate with blank value & 1st dataGrid with some value. When I click on EDIT button Of the 1st dataGrid,The Relevent data Of the particular row will populate in first 3 textboxs & corresponding relevent data Will populate in the 2nd DataGrid. Now when I click 2nd datagrid's EDIT button of a perticular Row,relevent data will populate on last 2 textboxs & 1st 3 textbox's value will be same as per as it was........ ........My Problem Is ........... When I click on edit button Of 2nd DataGrid, 1st 3 text boxes values goes Off & 2nd DataGrid Is goes off Due to Postback.but The relevent value is populated In last 2 textboxes.So How I stoped this postback??So that all the data in 1st 3 textboxes will be same.So that I can edit the values of 2nd Grid with 1st 3 textBoxes value.. plz dirctly mail me 1 copy of post to... maloy.adhikari@in.com

2 Answers  






What is the maximum amount of memory any single process on Windows can address? Is this different than the maximum virtual memory for the system? How would this affect a system design?

2 Answers  


what is the jit? what is the type of jit and purpose of it>

2 Answers   Honeywell,


Can anyone please tell me that the question posted on this website are Sufficient for the interview with 2+ year experience in .net

0 Answers  


How can we identify that the Page is Post Back?

0 Answers  


Differentiate between a page theme and a global theme?

0 Answers  


Is it possible to use two versions of assembly at the same time?If possible explain with code?

3 Answers   Tech Mahindra,


Suppose you want a certain ASP.NET function executed on MouseOver over a certain button. Where do you add an event handler?

1 Answers   Zenith,


Categories