Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Questions 1 and 2</strong>: The CLR basically checks whether or not the finalizer is overridden. If it's not, it treats it as not having a finalizer.</p> <p>The benefit of having a finalizer in System.Object is that compilers know they can <em>always</em> put a call to <code>base.Finalize()</code> in. This avoids versioning issues. Consider a world without <code>System.Object.Finalize()</code>:</p> <ul> <li>System.Object (no Finalize)</li> <li>Acme.BaseClass (no Finalize)</li> <li>MyCompany.DerivedClass (Finalize)</li> </ul> <p>Without a <code>Finalize</code> method in object, the finalizer in MyCompany.DerivedClass can't call anything. Which leads to a problem when version 2 of Acme.BaseClass comes out <em>with</em> a finalizer. Unless you recompile MyCompany.DerivedClass, an instance of DerivedClass will be finalized without calling BaseClass.Finalize, which is clearly a Bad Thing.</p> <p>Now consider the same situation <em>with</em> System.Object.Finalize - the compiler inserts a call to base.Finalize automatically in DerivedClass.Finalize, which in version 1 just calls the no-op implementation in System.Object. When version 2 of Acme.BaseClass comes out, the call to <code>base.Finalize</code> will (without recompilation of DerivedClass) call BaseClass.Finalize.</p> <p><strong>Question 3</strong>: No, you don't need to have a finalizer just because you implement IDisposable. Finalizers should only be used for unmanaged resources which <em>nothing else is going to clean up</em> - i.e. ones you have a <em>direct</em> reference to. For instance, suppose you have a class which has a <code>FileStream</code> member variable. You want to implement <code>IDisposable</code> so you can close the stream as soon as possible, if the caller remembers - but if they <em>don't</em> remember to call <code>Dispose()</code>, the stream will become eligible for garbage collection at the same time as your object. Trust that <code>FileStream</code> has an appropriate finalizer (or a reference to something else with a finalizer etc) rather than trying to clean it up in your own finalizer.</p> <p>As of .NET 2.0, with the <a href="http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.safehandle.aspx" rel="noreferrer">SafeHandle</a> class, it should be <em>incredibly</em> rare for you to need your own finalizer.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload