What is the Difference B/W Finalize() and Dispose() in .Net?
Answers were Sorted based on User's Feedback
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
Which keyword you should use for class not to extend? Which keyword to be used for making the class accessible within the assembly only what are the authentication mode of ASP.NET How will you call a JavaScript function using code-behind? How will you define the Session Timeout? What are the methods to validate client-side whether the input is a correct Date format? You want to write a log when ASP.NET application starts. Which is the best place to write that peice of code? What are the page load events and what is there order? What is the difference between abstract class and interface? What are the differences between DataSet and DataReader? What are the two properties other than DataSource, which is required to populate the Drop Down Menu? What is difference between String and StringBuilder Classes? What are differences between Custom control and User control?
How many webforms are possible on a single webpage?
How can you assign styles to webforms?
What is asp net objects?
Is there any alternative to avoid name collisions other then Namespaces?
Explain security types in asp.net?
when u enter the data in one text box once u completed entering the text box data then one page has to be popuped and the text has to be displayed in the parent page
What is the default timeout for a cookie?
Explain some of the major built-in objects in asp.net
What is the flow of processing of the request? : asp.net mvc
Define web.config in .net?
How to migrate from sqlserver 2005 to sql server 2000 (every one know reverse process of that )
Visual Basic (800)
C Sharp (3816)
ASP.NET (3180)
VB.NET (461)
COM+ (79)
ADO.NET (717)
IIS (369)
MTS (11)
Crystal Reports (81)
BizTalk (89)
Dot Net (2435)
Exchange Server (362)
SharePoint (720)
WCF (340)
MS Office Microsoft (6963)
LINQ Language-Integrated Query (317)
WPF (371)
TypeScript (144)
Microsoft Related AllOther (311)