Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET C#4.0
This is one of the interview questions... can we call dispose method inside finalize method because finalize() is called automatically by the Garbage Collector, so can we all dispose in finalize.
What's the difference between dispose and finalize?
Posted 23-Aug-10 21:15pm
Edited 24-Aug-10 9:13am
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You should consider that telling you the answer to this interview question will not raise your overall experience to the level that will get you a job.
 
Dispose is something you can call yourself. Finalise calls it for you if you failed to call it. Thus, the dispose method is used to give us control over cleaning up of resources.
  Permalink  
v2
Comments
Mastermanoj at 24-Aug-10 13:51pm
   
Yes Christian thanks a lot,but i am trying to raise my level.
we call dispose when we want.but if we are calling it inside finalize method then what happens since both does the cleaning of resources and finalize is called when garbage collector starts cleaning up of resource.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

1. Dispose is part of IDisposable interface and is called explicitly by invoking method Dispose or by "using" statement, which calls it automatically.
 
2. There is an article about it on MSDN:
http://msdn.microsoft.com/en-us/library/b1yfkh5e.aspx[^].
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Yes, we can call dispose method in Finalize().
 
By doing this, we force the dispose method to be called automatically.
 
We can call the dispose method in Finalize() and in Dispose method, suppress the finalize method using GC.SuppressFinalize.
 
Below is the sample code of the pattern. This is the best way we do clean our unallocated resources and yes not to forget we do not get the hit of running the Garbage collector twice. It will suppress the finalize method thus avoiding the two trips.
 
   Public Class ClsTesting
     Implements IDisposable
       Public Overloads Sub Dispose()Implements IDisposable.Dispose
         ' write ytour clean up code here
           GC.SuppressFinalize(Me)
       End Sub
       Protected Overrides Sub Finalize()
          Dispose()
       End Sub
    End Class
 
Hope this may help you. Smile | :)
  Permalink  
v3
Comments
Sandeep Mewara at 24-Aug-10 12:07pm
   
Just formatted the code part.
Mastermanoj at 24-Aug-10 13:56pm
   
Thanks a lot Sandesh,this has cleared my some of doubts.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 505
1 Maciej Los 325
2 Richard MacCutchan 265
3 Mathew Soji 220
4 BillWoodruff 210
0 OriginalGriff 8,804
1 Sergey Alexandrovich Kryukov 7,457
2 DamithSL 5,689
3 Maciej Los 5,279
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web03 | 2.8.1411028.1 | Last Updated 24 Aug 2010
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100