Re: Finalizer vs. Dispose

From: Jochen Kalmbach (nospam-Jochen.Kalmbach_at_holzma.de)
Date: 03/17/05


Date: Thu, 17 Mar 2005 06:08:50 -0800

Hi Klaus Aschenbrenner,

> When should we use a finalizer and when should we implement the
> IDisposable interface? I think when we have the IDisposable interface
> we don't need a finalizer - or are there any scenarios when a
> finalizer is better than implementing the IDisposable interface?

Finalizer and IDisposable are primarily two different things...

If a class implements a finalizer, it will (mostly) be called by the GC if
the object is not used anymore. The finalizer will also be called from
within a different thread...

IDisposable.Dispose is just a normal method!

In general you can say:
Implement IDisposable if you need to free unmanaged resources.

And additionally implement a finalizer if you want to free the unmanaged
resources if the user of your class forgot to call "Dispose".

Mostly all .NET classes which implements IDisposable, also implements an
finalizer.

-- 
Greetings
  Jochen
 
   My blog about Win32 and .NET
   http://blog.kalmbachnet.de/


Relevant Pages

  • Re: Finalizer vs. Dispose
    ... I think when we have the IDisposable interface ... >>finalizer is better than implementing the IDisposable interface? ... > IDisposable.Dispose is just a normal method! ... > Implement IDisposable if you need to free unmanaged resources. ...
    (microsoft.public.dotnet.framework.clr)
  • Re: Finalizer vs. Dispose
    ... I think when we have the IDisposable interface ... >>finalizer is better than implementing the IDisposable interface? ... > IDisposable.Dispose is just a normal method! ... > Implement IDisposable if you need to free unmanaged resources. ...
    (microsoft.public.dotnet.framework)
  • Re: Accessing private data during finalize/dispose
    ... > I understand the basics of finalization and implementing IDisposable and how one is guaranteed access to managed objects only when working through the IDisposable interface. ... class A provides management for the creation ... > protected Dispose(bool disposing) ... You can safely access other objects from a finalizer, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Finalizer vs. Dispose
    ... I think when we have the IDisposable interface ... > finalizer is better than implementing the IDisposable interface? ... Implement IDisposable if you need to free unmanaged resources. ... Mostly all .NET classes which implements IDisposable, ...
    (microsoft.public.dotnet.framework)
  • Re: Garbage collection problem
    ... > IDisposable interface and a using statement. ... I wouldn't call syntactic sugar for a try-finally statement a garbage ... that the contract for Dispose() in IDisposable says that object ... to call its finalizer. ...
    (comp.lang.java.programmer)

Loading